Skip to content

Commit bfbec53

Browse files
author
Bryan Clark
committed
log when we overwrite the file
1 parent c1c11bb commit bfbec53

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

dist/index.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auth.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export async function configAuthentication(
1616
console.log(
1717
`creating ${SETTINGS_FILE} with server-id: ${id}, username: ${username}, and a password`
1818
);
19-
const directory: string = path.join(os.homedir(), M2_DIR);
19+
const home: string = process.env['GITHUB_WORKSPACE'] || os.homedir();
20+
const directory: string = path.join(home, M2_DIR);
2021
await io.mkdirP(directory);
2122
core.debug(`created directory ${directory}`);
2223
await write(directory, generate(id, username, password));
@@ -43,8 +44,17 @@ export function generate(id: string, username: string, password: string) {
4344
}
4445

4546
async function write(directory: string, settings: string) {
46-
const options = {encoding: 'utf-8'};
47+
const options = {encoding: 'utf-8', flag: 'wx'}; // 'wx': Like 'w' but fails if path exists
4748
const location = path.join(directory, SETTINGS_FILE);
4849
console.log(`writing ${location}`);
49-
return fs.writeFileSync(location, settings, options);
50+
try {
51+
return fs.writeFileSync(location, settings, options);
52+
} catch (e) {
53+
if (e.code == fs.constants.O_EXCL) {
54+
console.log(`overwriting existing file ${location}`);
55+
// default flag is 'w'
56+
return fs.writeFileSync(location, settings, {encoding: 'utf-8'});
57+
}
58+
throw e;
59+
}
5060
}

0 commit comments

Comments
 (0)