From bfbec53132fb4c4369c9a47a2bd7aee544453438 Mon Sep 17 00:00:00 2001 From: Bryan Clark Date: Wed, 4 Dec 2019 23:54:21 -0500 Subject: [PATCH] log when we overwrite the file --- dist/index.js | 17 ++++++++++++++--- src/auth.ts | 16 +++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/dist/index.js b/dist/index.js index c01c0f3..32058f3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4134,7 +4134,8 @@ function configAuthentication(id, username, password) { return __awaiter(this, void 0, void 0, function* () { if (id && username && password) { console.log(`creating ${exports.SETTINGS_FILE} with server-id: ${id}, username: ${username}, and a password`); - const directory = path.join(os.homedir(), exports.M2_DIR); + const home = process.env['GITHUB_WORKSPACE'] || os.homedir(); + const directory = path.join(home, exports.M2_DIR); yield io.mkdirP(directory); core.debug(`created directory ${directory}`); yield write(directory, generate(id, username, password)); @@ -4162,10 +4163,20 @@ function generate(id, username, password) { exports.generate = generate; function write(directory, settings) { return __awaiter(this, void 0, void 0, function* () { - const options = { encoding: 'utf-8' }; + const options = { encoding: 'utf-8', flag: 'wx' }; // 'wx': Like 'w' but fails if path exists const location = path.join(directory, exports.SETTINGS_FILE); console.log(`writing ${location}`); - return fs.writeFileSync(location, settings, options); + try { + return fs.writeFileSync(location, settings, options); + } + catch (e) { + if (!e.code) + throw e; + if (e.code == fs.constants.O_EXCL) { + console.log(`overwriting existing file ${location}`); + return fs.writeFileSync(location, settings, { encoding: 'utf-8' }); + } + } }); } diff --git a/src/auth.ts b/src/auth.ts index b61b357..875c6b4 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -16,7 +16,8 @@ export async function configAuthentication( console.log( `creating ${SETTINGS_FILE} with server-id: ${id}, username: ${username}, and a password` ); - const directory: string = path.join(os.homedir(), M2_DIR); + const home: string = process.env['GITHUB_WORKSPACE'] || os.homedir(); + const directory: string = path.join(home, M2_DIR); await io.mkdirP(directory); core.debug(`created directory ${directory}`); await write(directory, generate(id, username, password)); @@ -43,8 +44,17 @@ export function generate(id: string, username: string, password: string) { } async function write(directory: string, settings: string) { - const options = {encoding: 'utf-8'}; + const options = {encoding: 'utf-8', flag: 'wx'}; // 'wx': Like 'w' but fails if path exists const location = path.join(directory, SETTINGS_FILE); console.log(`writing ${location}`); - return fs.writeFileSync(location, settings, options); + try { + return fs.writeFileSync(location, settings, options); + } catch (e) { + if (e.code == fs.constants.O_EXCL) { + console.log(`overwriting existing file ${location}`); + // default flag is 'w' + return fs.writeFileSync(location, settings, {encoding: 'utf-8'}); + } + throw e; + } }