Skip to content

Commit

Permalink
Add eslint-plugin-n
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Dec 3, 2023
1 parent 0c4eb52 commit e57a3ad
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": ["jc"],
"extends": ["jc", "jc/typescript-typecheck", "jc/node"],
"parserOptions": {
"project": true
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-jsx-a11y": "^6.8.0",
"eslint-plugin-n": "^16.3.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-regexp": "^2.1.1",
Expand Down
73 changes: 43 additions & 30 deletions packages/create-jc-project/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,47 @@ if (process.argv.includes("--version") || process.argv.includes("-v")) {
}

console.log(pico.cyan("Starting to scaffold new JC project..."));
const { name, description, usingReact } = await prompts(
[
{
type: "text",
name: "name",
message: "What should we call this project?",
async validate(value) {
if (
typeof value !== "string" ||
!/^(?:@[a-z-]+\/)?[a-z-]+$/u.test(value)
)
return "Not a valid package name.";
if (await pathExists(value)) return "The directory already exists.";
return true;
function parseValidRes(r: { [key: string]: unknown }) {
if (
typeof r.name !== "string" ||
typeof r.description !== "string" ||
typeof r.usingReact !== "boolean"
) {
console.log(pico.red("Invalid input."));
process.exit(1);
}
return r as { name: string; description: string; usingReact: boolean };
}
const { name, description, usingReact } = parseValidRes(

Check warning on line 31 in packages/create-jc-project/src/index.ts

View workflow job for this annotation

GitHub Actions / lint

'name' is already a global variable
await prompts(
[
{
type: "text",
name: "name",
message: "What should we call this project?",
async validate(value) {
if (
typeof value !== "string" ||
!/^(?:@[a-z-]+\/)?[a-z-]+$/u.test(value)
)
return "Not a valid package name.";
if (await pathExists(value)) return "The directory already exists.";
return true;
},
},
{
type: "text",
name: "description",
message: "Brief description?",
},
},
{
type: "text",
name: "description",
message: "Brief description?",
},
{
type: "confirm",
name: "usingReact",
message: "Using React?",
},
],
{ onCancel: () => process.exit(0) },
{
type: "confirm",
name: "usingReact",
message: "Using React?",
},
],
{ onCancel: () => process.exit(0) },
),
);
const packageJSON = {
name,
Expand Down Expand Up @@ -76,10 +89,10 @@ async function rReadDir(dir: string): Promise<[string, Buffer][]> {
const dirents = await fs.readdir(dir, { withFileTypes: true });
const files = await Promise.all(
dirents.map(async (dirent) => {
const res = path.join(dir, dirent.name);
const subDir = path.join(dir, dirent.name);
return dirent.isDirectory()
? rReadDir(res)
: [[res, await fs.readFile(res)] as [string, Buffer]];
? rReadDir(subDir)
: [[subDir, await fs.readFile(subDir)] as [string, Buffer]];
}),
);
return files.flat();
Expand Down
4 changes: 3 additions & 1 deletion packages/create-jc-project/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export function exec(
if (((e): e is SpawnSyncReturns<string> => true)(err)) {
if (err.stdout) console.log(err.stdout);
if (err.stderr) console.log(err.stderr);
console.log(pico.red(`Command exited with code ${err.status}.`));
console.log(
pico.red(`Command exited with code ${err.status ?? "unknown"}.`),
);
if (index < times - 1) console.log(pico.cyan("Retrying..."));
}
return false;
Expand Down
6 changes: 4 additions & 2 deletions packages/eslint-config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ module.exports = {
"./rules/typescript.js",
"./rules/import.js",
"./rules/react.js",
// Not included by default
"./rules/jsx.js",
// Not included by default:
// "./rules/typescript-typecheck.js",
// "./rules/node.js",
// "./rules/react-class-comps.js",
// "./rules/react-prop-types.js",
"./rules/jsx.js",
],
};
1 change: 1 addition & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"./base": "./rules/base.js",
"./regex": "./rules/regex.js",
"./import": "./rules/import.js",
"./node": "./rules/node.js",
"./jsx": "./rules/jsx.js",
"./react": "./rules/react.js",
"./react-class-comps": "./rules/react-class-comps.js",
Expand Down
84 changes: 84 additions & 0 deletions packages/eslint-config/rules/node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module.exports = {
plugins: ["n"],
rules: {
// Callback-related
"n/callback-return": "off",

"n/exports-style": ["error", "module.exports", { allowBatchAssign: false }],

// Checked by TypeScript
"n/file-extension-in-import": "off",

// On-demand loading is useful
"n/global-require": "off",

// Callback-related
"n/handle-callback-err": "off",

// Callback-related
"n/no-callback-literal": "off",

"n/no-deprecated-api": "error",

"n/no-exports-assign": "error",

// Shadowed by import/no-extraneous-dependencies
"n/no-extraneous-import": "off",

// Shadowed by import/no-extraneous-dependencies
"n/no-extraneous-require": "off",

// Checked by TypeScript
"n/no-missing-import": "off",

// Checked by TypeScript
"n/no-missing-require": "off",

// Not helpful
"n/no-mixed-requires": "off",

"n/no-new-require": "error",

"n/no-path-concat": "warn",

"n/no-process-env": "off",

// Useful in theory, but too noisy
"n/no-process-exit": "off",

"n/no-restricted-import": "off",

"n/no-restricted-require": "off",

"n/no-sync": ["warn", { allowAtRootLevel: false }],

"n/no-unpublished-bin": "error",

"n/no-unpublished-import": "off",

"n/no-unpublished-require": "off",

"n/prefer-global/buffer": ["error", "always"],

"n/prefer-global/console": ["error", "always"],

"n/prefer-global/process": ["error", "always"],

"n/prefer-global/text-decoder": ["error", "always"],

"n/prefer-global/text-encoder": ["error", "always"],

"n/prefer-global/url": ["error", "always"],

"n/prefer-global/url-search-params": ["error", "always"],

"n/prefer-promises/dns": "error",

"n/prefer-promises/fs": "error",

"n/process-exit-as-throw": "warn",

// Errors on TypeScript bin
"n/shebang": "off",
},
};
Loading

0 comments on commit e57a3ad

Please sign in to comment.