Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@
"app:uninstall": "APUI"
}
}
}
}
3 changes: 2 additions & 1 deletion src/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
getAppName,
getDirName,
getOrgAppUiLocation,
sanitizePath
} from "../../util";

export default class Create extends BaseCommand<typeof Create> {
Expand Down Expand Up @@ -386,7 +387,7 @@ export default class Create extends BaseCommand<typeof Create> {

this.sharedConfig.folderPath = resolve(
dirname(this.sharedConfig.folderPath),
this.appData.name
sanitizePath(this.appData.name)
);
this.sharedConfig.nameChanged = true;

Expand Down
5 changes: 5 additions & 0 deletions src/util/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ async function fetchInstalledApps(
return batchRequests.flat();
}


// To remove the relative path
const sanitizePath = (str: string) => str?.replace(/^(\.\.(\/|\\|$))+/, '');

export {
getOrganizations,
getOrgAppUiLocation,
Expand All @@ -237,4 +241,5 @@ export {
fetchStack,
uninstallApp,
fetchInstalledApps,
sanitizePath
};
15 changes: 8 additions & 7 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { existsSync, readdirSync, writeFileSync, mkdirSync } from "fs";
import { resolve } from "path";
import config from "../config";
import messages, {$t} from "../messages";
import messages, { $t } from "../messages";
import { LogFn } from "../types";
import { cliux } from "@contentstack/cli-utilities";
import { sanitizePath } from './common-utils'

export async function writeFile(dir: string=process.cwd(), force: boolean=false, data: Record<string, any> | undefined={}, log: LogFn=console.log) {
export async function writeFile(dir: string = process.cwd(), force: boolean = false, data: Record<string, any> | undefined = {}, log: LogFn = console.log) {
await ensureDirectoryExists(dir)
const files = readdirSync(dir)
const latestFileName = files.filter(fileName => fileName.match(new RegExp(config.defaultAppFileName))).pop()?.split('.')[0] || config.defaultAppFileName;
let target = resolve(dir, `${latestFileName}.json`)
let target = resolve(sanitizePath(dir), `${sanitizePath(latestFileName)}.json`)
if (existsSync(target)) {
const userConfirmation: boolean = force || (await cliux.confirm($t(messages.FILE_ALREADY_EXISTS, { file: `${config.defaultAppFileName}.json` })))
if (userConfirmation) {
target = resolve(dir, `${config.defaultAppFileName}.json`);
target = resolve(sanitizePath(dir), `${sanitizePath(config.defaultAppFileName)}.json`);
} else {
target = resolve(dir, `${incrementName(latestFileName)}.json`);
target = resolve(sanitizePath(dir), `${sanitizePath(incrementName(latestFileName))}.json`);
}
}
await writeFileSync(target, JSON.stringify(data))
Expand All @@ -24,10 +25,10 @@ export async function writeFile(dir: string=process.cwd(), force: boolean=false,

async function ensureDirectoryExists(dir: string) {
if (!existsSync(dir)) {
await mkdirSync(dir, {recursive: true})
await mkdirSync(dir, { recursive: true })
}
}

function incrementName(name: string) {
return `${config.defaultAppFileName}${Number(name.split(config.defaultAppFileName).pop())+1}`
return `${config.defaultAppFileName}${Number(name.split(config.defaultAppFileName).pop()) + 1}`
}
5 changes: 3 additions & 2 deletions src/util/inquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
fetchAppInstallations,
fetchInstalledApps,
fetchApps,
sanitizePath,
} from "./common-utils";

/**
Expand Down Expand Up @@ -64,14 +65,14 @@ async function getDirName(path: string): Promise<string> {
return $t(errors.INVALID_NAME, { min: "3", max: "50" });
}

if (existsSync(join(basePath, name))) {
if (existsSync(join(sanitizePath(basePath), sanitizePath(name)))) {
return messages.DIR_EXIST;
}

return true;
},
})
.then((name) => join(basePath, name as string));
.then((name) => join(sanitizePath(basePath), sanitizePath(name as string)));
}

/**
Expand Down