Skip to content

Commit

Permalink
Run lint
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Jul 25, 2024
1 parent fc33468 commit a2608ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 58 deletions.
2 changes: 0 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": true,
"bracketSpacing": false,
"arrowParens": "always"
}
90 changes: 45 additions & 45 deletions packages/create-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { existsSync } from "node:fs";
import { rm, readFile, writeFile, mkdir } from "node:fs/promises";
import { resolve } from "node:path";
import prompts from "prompts";
import { green, bold, dim, yellow } from "kolorist";
import { existsSync } from 'node:fs';
import { rm, readFile, writeFile, mkdir } from 'node:fs/promises';
import { resolve } from 'node:path';
import prompts from 'prompts';
import { green, bold, dim, yellow } from 'kolorist';
import {
copyDir,
emptyDir,
isEmpty,
removePkgDependencies,
removePkgFields,
toValidPkgName,
} from "./utils";
} from './utils';

/** List of relative paths in the template to be _removed_ from new projects. */
const TEMPLATE_EXCLUDE_PATHS = ["node_modules", "scripts"];
const TEMPLATE_EXCLUDE_PATHS = ['node_modules', 'scripts'];

/** List of dependencies in the template to be _removed_ from new projects. */
const TEMPLATE_EXCLUDE_DEPS = ["@carto/create-common"];
const TEMPLATE_EXCLUDE_DEPS = ['@carto/create-common'];

/**
* List of package.json fields to clear from new projects.
* See: https://docs.npmjs.com/cli/v10/configuring-npm/package-json
*/
const TEMPLATE_EXCLUDE_PKG_FIELDS = [
"author",
"bin",
"bugs",
"description",
"files",
"homepage",
"keywords",
"license",
"publishConfig",
"repository",
"version",
'author',
'bin',
'bugs',
'description',
'files',
'homepage',
'keywords',
'license',
'publishConfig',
'repository',
'version',
];

/**
Expand All @@ -49,11 +49,11 @@ export async function createProject(
const targetDir = resolve(process.cwd(), inputTargetDir);

console.log(`
${green("✔")} ${bold("Template directory")} ${dim("…")} ${templateDir}
${green("✔")} ${bold("Target directory")} ${dim("…")} ${targetDir}
${green('✔')} ${bold('Template directory')} ${dim('…')} ${templateDir}
${green('✔')} ${bold('Target directory')} ${dim('…')} ${targetDir}
`);

console.log(dim("…\n"));
console.log(dim('…\n'));

/****************************************************************************
* Validate target directory.
Expand All @@ -68,8 +68,8 @@ ${green("✔")} ${bold("Target directory")} ${dim("…")} ${targetDir}
if (existsSync(targetDir) && !isEmpty(targetDir)) {
const { overwrite } = await prompts([
{
type: "confirm",
name: "overwrite",
type: 'confirm',
name: 'overwrite',
message: `Target directory "${targetDir}" is not empty. Overwrite?`,
},
]);
Expand All @@ -89,22 +89,22 @@ ${green("✔")} ${bold("Target directory")} ${dim("…")} ${targetDir}
const config = await prompts(
[
{
name: "title",
type: "text",
message: "Title for the application",
validate: (text) => (text.length === 0 ? "Title is required" : true),
name: 'title',
type: 'text',
message: 'Title for the application',
validate: (text) => (text.length === 0 ? 'Title is required' : true),
},
{
name: "accessToken",
type: "password",
message: "Access token for CARTO API",
name: 'accessToken',
type: 'password',
message: 'Access token for CARTO API',
validate: (text) =>
text.length === 0 ? "Access token is required" : true,
text.length === 0 ? 'Access token is required' : true,
},
{
name: "apiBaseUrl",
type: "text",
message: "Base URL for CARTO API (optional)",
name: 'apiBaseUrl',
type: 'text',
message: 'Base URL for CARTO API (optional)',
},
],
{
Expand All @@ -115,7 +115,7 @@ ${green("✔")} ${bold("Target directory")} ${dim("…")} ${targetDir}
},
);

console.log(dim("\n…"));
console.log(dim('\n…'));

/****************************************************************************
* Populate project directory.
Expand All @@ -134,8 +134,8 @@ ${green("✔")} ${bold("Target directory")} ${dim("…")} ${targetDir}
}

// Set up package.json.
const pkgPath = resolve(targetDir, "package.json");
const pkg = JSON.parse(await readFile(pkgPath, "utf8"));
const pkgPath = resolve(targetDir, 'package.json');
const pkg = JSON.parse(await readFile(pkgPath, 'utf8'));
removePkgDependencies(pkg, TEMPLATE_EXCLUDE_DEPS);
removePkgFields(pkg, TEMPLATE_EXCLUDE_PKG_FIELDS);
pkg.name = toValidPkgName(config.title);
Expand All @@ -145,20 +145,20 @@ ${green("✔")} ${bold("Target directory")} ${dim("…")} ${targetDir}
// TODO(feat): Populate title, access token, and api base URL.

// Create empty yarn.lock. Required when working in sandbox/.
await writeFile(resolve(targetDir, "yarn.lock"), "");
await writeFile(resolve(targetDir, 'yarn.lock'), '');

// Suggest next steps
const steps = [
...(inputTargetDir !== "." ? [`${dim("$")} cd ${inputTargetDir}`] : []),
`${dim("$")} yarn`,
`${dim("$")} yarn dev`,
...(inputTargetDir !== '.' ? [`${dim('$')} cd ${inputTargetDir}`] : []),
`${dim('$')} yarn`,
`${dim('$')} yarn dev`,
];

console.log(`
${green("✔")} ${bold(`Project "${config.title}" was created!`)}
${green('✔')} ${bold(`Project "${config.title}" was created!`)}
${bold(yellow("!"))} ${bold("Next steps")}:
${bold(yellow('!'))} ${bold('Next steps')}:
${steps.join("\n")}
${steps.join('\n')}
`);
}
22 changes: 11 additions & 11 deletions packages/create-common/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { rm, stat, copyFile, mkdir, readdir } from "node:fs/promises";
import { resolve } from "node:path";
import { rm, stat, copyFile, mkdir, readdir } from 'node:fs/promises';
import { resolve } from 'node:path';

/******************************************************************************
* Disk utilities.
Expand Down Expand Up @@ -27,12 +27,12 @@ export async function copyDir(srcDir: string, destDir: string): Promise<void> {

export async function isEmpty(path: string): Promise<boolean> {
const files = await readdir(path);
return files.length === 0 || (files.length === 1 && files[0] === ".git");
return files.length === 0 || (files.length === 1 && files[0] === '.git');
}

export async function emptyDir(dir: string): Promise<void> {
for (const file of await readdir(dir)) {
if (file === ".git") {
if (file === '.git') {
continue;
}
await rm(resolve(dir, file), { recursive: true, force: true });
Expand All @@ -55,20 +55,20 @@ export function toValidPkgName(projectName: string) {
return projectName
.trim()
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/^[._]/, "")
.replace(/[^a-z\d\-~]+/g, "-");
.replace(/\s+/g, '-')
.replace(/^[._]/, '')
.replace(/[^a-z\d\-~]+/g, '-');
}

export function removePkgDependencies<T extends Record<string, unknown>>(
pkg: T,
excludeDeps: string[],
): T {
const dependencyTypes = [
"dependencies",
"devDependencies",
"optionalDependencies",
"peerDependencies",
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
];

for (const exclude of excludeDeps) {
Expand Down

0 comments on commit a2608ba

Please sign in to comment.