Skip to content

Commit

Permalink
fix; JSON parse error on exit, if if_key_exists=fail and key exists
Browse files Browse the repository at this point in the history
  • Loading branch information
shimataro committed Oct 11, 2023
1 parent d05e1bd commit f782e91
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 49 deletions.
28 changes: 14 additions & 14 deletions dist/main.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/post.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ const STATE_CREATED_FILES = "created-files";
/**
* create backup suffix name
* @param dirName directory to back up
* @returns backup suffix
* @returns backup suffix; empty string if directory does not exist
*/
export function createBackupSuffix(dirName: string): string {
if (!fs.existsSync(dirName)) {
// do nothing if directory does not exist
return "";
}

Expand Down Expand Up @@ -47,6 +46,10 @@ export function saveCreatedFileNames(fileNames: string[]): void {
*/
export function loadCreatedFileNames(): string[] {
const json = core.getState(STATE_CREATED_FILES);
if (json === "") {
return [];
}

return JSON.parse(json) as string[];
}

Expand Down
67 changes: 40 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,44 @@ try {
* main function
*/
export function main(): void {
const sshDirName = common.getSshDirectory();

// files to be created
const files = buildFilesToCreate(sshDirName);

// create ".ssh" directory
const backupSuffix = common.createBackupSuffix(sshDirName);
if (backupSuffix === "") {
createDirectory(sshDirName);
console.log(`✅SSH directory "${sshDirName}" has been created successfully.`);
}

// back up & create files
const createdFileNames: string[] = [];
const backedUpFileNames: string[] = [];
for (const file of files) {
const pathName = path.join(sshDirName, file.name);
if (backup(pathName, backupSuffix, file.mustNotExist)) {
backedUpFileNames.push(file.name);
}

fs.writeFileSync(pathName, file.contents, file.options);
createdFileNames.push(file.name);
}
common.saveCreatedFileNames(createdFileNames);

console.log(`✅Following files have been created in "${sshDirName}" successfully; ${createdFileNames.join(", ")}`);
if (backedUpFileNames.length > 0) {
console.log(`✅Following files have been backed up in suffix "${backupSuffix}" successfully; ${backedUpFileNames.join(", ")}`);
}
}

/**
* build files to create
* @param dirName directory name in where files will be created
* @returns files
*/
function buildFilesToCreate(dirName: string): FileInfo[] {
// parameters
const key = core.getInput("key", {
required: true,
Expand All @@ -45,14 +83,6 @@ export function main(): void {
const config = core.getInput("config");
const ifKeyExists = core.getInput("if_key_exists");

// create ".ssh" directory
const sshDirName = common.getSshDirectory();
const backupSuffix = common.createBackupSuffix(sshDirName);
if (backupSuffix === "") {
createDirectory(sshDirName);
console.log(`✅SSH directory "${sshDirName}" has been created successfully.`);
}

// files to be created
const files: FileInfo[] = [
{
Expand All @@ -65,7 +95,7 @@ export function main(): void {
mustNotExist: false,
},
];
if (shouldCreateKeyFile(path.join(sshDirName, name), ifKeyExists)) {
if (shouldCreateKeyFile(path.join(dirName, name), ifKeyExists)) {
files.push({
name: name,
contents: insertLf(key, false, true),
Expand All @@ -88,24 +118,7 @@ export function main(): void {
});
}

// create files
const createdFileNames: string[] = [];
const backedUpFileNames: string[] = [];
for (const file of files) {
const fileName = path.join(sshDirName, file.name);
if (backup(fileName, backupSuffix, file.mustNotExist)) {
backedUpFileNames.push(file.name);
}

fs.writeFileSync(fileName, file.contents, file.options);
createdFileNames.push(file.name);
}
common.saveCreatedFileNames(createdFileNames);

console.log(`✅Following files have been created in "${sshDirName}" successfully; ${createdFileNames.join(", ")}`);
if (backedUpFileNames.length > 0) {
console.log(`✅Following files have been backed up in suffix "${backupSuffix}" successfully; ${backedUpFileNames.join(", ")}`);
}
return files;
}

/**
Expand Down

0 comments on commit f782e91

Please sign in to comment.