Skip to content

Commit

Permalink
✨ Allow user to customize package name
Browse files Browse the repository at this point in the history
  • Loading branch information
siguici committed Sep 30, 2024
1 parent d84f8ca commit 36c14a5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
3 changes: 2 additions & 1 deletion libs/create-qwikdev-astro/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"noParameterAssign": "off"
},
"suspicious": {
"noExplicitAny": "off"
"noExplicitAny": "off",
"noMisleadingCharacterClass": "off"
}
}
},
Expand Down
74 changes: 65 additions & 9 deletions libs/create-qwikdev-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,18 @@ export const clearDir = async (dir: string) => {
);
};

export function fileGetContents(file: string): string {
function fileGetContents(file: string): string {
if (!fs.existsSync(file)) {
throw new Error(`File ${file} not found`);
}
return fs.readFileSync(file, { encoding: "utf8" }).toString();
}

export function filePutContents(file: string, contents: string) {
function filePutContents(file: string, contents: string) {
return fs.writeFileSync(file, contents, { encoding: "utf8" });
}

export function fileReplaceContents(
file: string,
search: string | RegExp,
replace: string
) {
function fileReplaceContents(file: string, search: string | RegExp, replace: string) {
let contents = fileGetContents(file);
contents = contents.replace(search, replace);
filePutContents(file, contents);
Expand All @@ -215,8 +211,52 @@ export function pmRunCommand(): string {
return pm;
}

export function replacePackageJsonRunCommand(dir: string) {
fileReplaceContents(join(dir, "package.json"), /npm run/g, pmRunCommand());
export function getPackageJsonPath(dir = __dirname): string {
return join(dir, "package.json");
}

function packageJsonReplace(dir: string, search: string | RegExp, replace: string) {
fileReplaceContents(getPackageJsonPath(dir), search, replace);
}

function replacePackageJsonRunCommand(dir: string) {
packageJsonReplace(dir, /npm run/g, pmRunCommand());
}

const npmPackageNamePattern =
/^(?:(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*)$/;

function sanitizePackageName(name: string): string {
name = name.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
name = name.replace(/[^a-zA-Z0-9\-._~/@]/g, "-");
name = name.replace(/^[-.]+|[-.]+$/g, "");
name = name.replace(/[-.]{2,}/g, "-");
name = name.toLowerCase();

return name;
}

function isValidPackageName(name: string): boolean {
return npmPackageNamePattern.test(name);
}

function validatePackageName(name: string): string {
name = sanitizePackageName(name);

if (!isValidPackageName(name)) {
throw new Error(`Invalid package name: ${name}`);
}

return name;
}

function updatePackageName(newName: string, dir = __dirname): void {
const packageJsonPath = getPackageJsonPath(dir);
const packageJson = JSON.parse(fileGetContents(packageJsonPath));
const cleanedName = validatePackageName(newName);

packageJson.name = cleanedName;
filePutContents(packageJsonPath, JSON.stringify(packageJson, null, 2));
}

export function panicCanceled() {
Expand Down Expand Up @@ -449,6 +489,22 @@ export async function createProject(config: ProjectConfig, defaultProject: strin
cpSync(templatePath, outDir, { recursive: true });
}

const defaultPackageName = sanitizePackageName(projectAnswer as string);
const packageName = config.it
? await text({
message: "What should be the name of this package?",
defaultValue: defaultPackageName,
placeholder: defaultPackageName
})
: defaultPackageName;

if (typeof packageName === "symbol" || isCancel(packageName)) {
panicCanceled();
}

updatePackageName(packageName as string, outDir);
log.info(`Updated package name to "${packageName as string}" 📦️`);

if (packageManager !== "npm") {
log.info(`Replacing 'npm run' by '${pmRunCommand()}' in package.json...`);
replacePackageJsonRunCommand(outDir);
Expand Down

0 comments on commit 36c14a5

Please sign in to comment.