Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
96 lines
3.1 KiB
TypeScript
96 lines
3.1 KiB
TypeScript
/*
|
|
Component Scaffolding Script
|
|
This is a script that enables scaffolding a new JSS component using `jss scaffold <ComponentName>`.
|
|
The default convention is that component names must start with a capital letter, and can contain
|
|
letters, number, underscores, or dashes.
|
|
|
|
If the <ComponentName> parameter includes a path, it must be relative to the src/components folder.
|
|
For example, `jss scaffold search/SearchBox` will create a component called `SearchBox` in
|
|
`src/components/search/SearchBox.tsx`. Specifying a relative path is optional, and just providing
|
|
the name is ok.
|
|
|
|
Edit this script if you wish to use your own conventions for component storage in your JSS app.
|
|
*/
|
|
|
|
/* eslint-disable no-throw-literal,no-console */
|
|
|
|
import fs from "fs";
|
|
import path from "path";
|
|
import chalk from "chalk";
|
|
import generateComponentSrc from "./templates/component-src";
|
|
|
|
const componentRootPath = "src/components";
|
|
|
|
// Matches component names that start with a capital letter, and contain only letters, number,
|
|
// underscores, or dashes. Optionally, the component name can be preceded by a relative path
|
|
const nameParamFormat = new RegExp(/^((?:[\w-]+\/)*)([A-Z][\w-]+)$/);
|
|
const componentArg = process.argv[2];
|
|
|
|
if (!componentArg) {
|
|
throw "Component name was not passed. Usage: jss scaffold <ComponentName>";
|
|
}
|
|
|
|
const regExResult = nameParamFormat.exec(componentArg);
|
|
|
|
if (regExResult === null) {
|
|
throw `Component name should start with an uppercase letter and contain only letters, numbers,
|
|
dashes, or underscores. If specifying a path, it must be relative to src/components`;
|
|
}
|
|
|
|
const componentPath = regExResult[1];
|
|
const componentName = regExResult[2];
|
|
const filename = `${componentName}.tsx`;
|
|
|
|
/**
|
|
* Force to use `crlf` line endings, we are using `crlf` across the project.
|
|
* Replace: `lf` (\n), `cr` (\r)
|
|
* @param {string} content
|
|
*/
|
|
function editLineEndings(content: string) {
|
|
return content.replace(/\r|\n/gm, "\r\n");
|
|
}
|
|
|
|
/**
|
|
* Creates a file relative to the specified path if the file doesn't exist. Creates directories as needed.
|
|
* @param {string} rootPath - the root path
|
|
* @param {string} fileContent - the file content
|
|
* @param {string} filename - the filename
|
|
* @returns the new file's filepath
|
|
*/
|
|
function scaffoldFile(
|
|
rootPath: string,
|
|
fileContent: string,
|
|
filename: string,
|
|
): string | null {
|
|
const outputDir = path.join(rootPath, componentPath);
|
|
const outputFile = path.join(outputDir, filename);
|
|
|
|
if (fs.existsSync(outputFile)) {
|
|
console.log(chalk.red(`Skipping creating ${outputFile}; already exists.`));
|
|
return null;
|
|
}
|
|
|
|
fs.mkdirSync(outputDir, { recursive: true });
|
|
fs.writeFileSync(outputFile, editLineEndings(fileContent), "utf8");
|
|
console.log(chalk.green(`File ${outputFile} has been scaffolded.`));
|
|
return outputFile;
|
|
}
|
|
|
|
const componentOutputPath = scaffoldFile(
|
|
componentRootPath,
|
|
generateComponentSrc(componentName),
|
|
filename,
|
|
);
|
|
|
|
console.log(
|
|
chalk.green(`
|
|
Scaffolding of ${componentName} complete.
|
|
Next steps:`),
|
|
);
|
|
|
|
if (componentOutputPath) {
|
|
console.log(
|
|
`* Implement the React component in ${chalk.green(componentOutputPath)}`,
|
|
);
|
|
}
|