Skip to content

Commit 34abd4f

Browse files
committed
Switch to package object, add ESLint and Prettier
1 parent 9ee1b6e commit 34abd4f

File tree

2 files changed

+93
-46
lines changed

2 files changed

+93
-46
lines changed

packages/create-utility/index.js

Lines changed: 92 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,49 @@ import { exec as cpExec } from "child_process";
77

88
const exec = promisify(cpExec);
99

10-
const packageJson = `{
11-
"name": "@hkamran/utility-{{name}}",
12-
"version": "0.1.0",
13-
"description": "{{description}}",
14-
"main": "index.js",
15-
"types": "index.d.ts",
16-
"type": "module",
17-
"scripts": {
18-
"prepare": "tsc --declaration --emitDeclarationOnly --allowJs index.js",
19-
"test": "echo \\"Error: no test specified\\" && exit 1"
10+
const packageJson = {
11+
name: "@hkamran/utility-{{name}}",
12+
version: "0.1.0",
13+
description: "",
14+
main: "index.js",
15+
types: "index.d.ts",
16+
type: "module",
17+
scripts: {
18+
lint: "eslint --ext .js,.ts --fix",
19+
format: "prettier . --write",
20+
prepare:
21+
"tsc --declaration --emitDeclarationOnly --allowJs index.js && pnpm lint && pnpm format",
22+
test: 'echo "Error: no test specified" && exit 1',
2023
},
21-
"author": {
22-
"name": "H. Kamran",
23-
"email": "[email protected]",
24-
"url": "https://hkamran.com"
24+
author: {
25+
name: "H. Kamran",
26+
27+
url: "https://hkamran.com",
2528
},
26-
"repository": {
27-
"type": "git",
28-
"url": "git+https://github.com/hkamran80/utilities-js.git",
29-
"directory": "packages/{{name}}"
29+
repository: {
30+
type: "git",
31+
url: "git+https://github.com/hkamran80/utilities-js.git",
32+
directory: "packages/{{name}}",
3033
},
31-
"keywords": [{{keywords}}],
32-
"license": "AGPL-3.0-or-later",
33-
"bugs": {
34-
"url": "https://github.com/hkamran80/utilities-js/issues?q=is%3Aopen+label%3A%22utility%3A+{{name}}%22+sort%3Aupdated-desc"
34+
keywords: [],
35+
license: "AGPL-3.0-or-later",
36+
bugs: {
37+
url: "https://github.com/hkamran80/utilities-js/issues?q=is%3Aopen+label%3A%22utility%3A+{{name}}%22+sort%3Aupdated-desc",
3538
},
36-
"homepage": "https://github.com/hkamran80/utilities-js/tree/main/packages/{{name}}#readme",
37-
"dependencies": {},
38-
"devDependencies": {
39-
"typescript": "^4.6.2"
40-
}
41-
}`;
39+
homepage:
40+
"https://github.com/hkamran80/utilities-js/tree/main/packages/{{name}}#readme",
41+
publishConfig: {
42+
access: "public",
43+
},
44+
devDependencies: {
45+
"@hkamran/prettier-config": "^1.1.1",
46+
eslint: "^8.36.0",
47+
"eslint-config-prettier": "^8.7.0",
48+
prettier: "^2.8.4",
49+
typescript: "^4.6.2",
50+
},
51+
prettier: "@hkamran/prettier-config",
52+
};
4253

4354
const indexJs = `console.log("Hello world!");`;
4455

@@ -52,10 +63,40 @@ Create a utility
5263
## Usage
5364
5465
\`\`\`bash
55-
npx @hkamran/utility-{{name}}
66+
npm i @hkamran/utility-{{name}}
67+
\`\`\`
68+
69+
\`\`\`bash
70+
pnpm add @hkamran/utility-{{name}}
71+
\`\`\`
72+
73+
\`\`\`bash
74+
yarn add @hkamran/utility-{{name}}
5675
\`\`\`
5776
`;
5877

78+
const ignore = `node_modules/*
79+
.gitignore
80+
.dccache
81+
*.md
82+
83+
**/dist/*
84+
**/node_modules/*
85+
**/.gitignore
86+
**/.dccache
87+
**/*.md`;
88+
89+
const eslintConfig = `module.exports = {
90+
root: true,
91+
env: { node: true },
92+
parserOptions: {
93+
ecmaVersion: "latest",
94+
sourceType: "module",
95+
},
96+
extends: ["eslint:recommended", "prettier"],
97+
};
98+
`;
99+
59100
const questions = [
60101
{
61102
type: "list",
@@ -85,22 +126,27 @@ const questions = [
85126

86127
const answers = await inquirer.prompt(questions);
87128

88-
await writeFile(
89-
"package.json",
90-
packageJson
91-
.replace(/{{name}}/gm, answers.name.toLowerCase().replace(/ /gm, "-"))
92-
.replace(/{{description}}/gm, answers.description)
93-
.replace(
94-
/{{keywords}}/gm,
95-
answers.keywords.length > 0
96-
? answers.keywords
97-
.toLowerCase()
98-
.split(",")
99-
.map((keyword) => `"${keyword}"`)
100-
.join(",")
101-
: "",
102-
),
129+
/** @type {string} */
130+
const packageName = answers.name.toLowerCase().replace(/ /gm, "-");
131+
132+
packageJson.name = packageJson.name.replace(/{{name}}/gm, packageName);
133+
packageJson.repository.directory = packageJson.repository.directory.replace(
134+
/{{name}}/gm,
135+
packageName,
103136
);
137+
packageJson.bugs.url = packageJson.bugs.url.replace(/{{name}}/gm, packageName);
138+
packageJson.homepage = packageJson.homepage.replace(/{{name}}/gm, packageName);
139+
140+
packageJson.description = answers.description;
141+
packageJson.keywords =
142+
answers.keywords.length > 0
143+
? answers.keywords.toLowerCase().split(",")
144+
: [];
145+
146+
await writeFile("package.json", JSON.stringify(packageJson));
147+
await writeFile(".eslintignore", ignore);
148+
await writeFile(".prettierignore", ignore);
149+
await writeFile(".eslintrc.js", eslintConfig);
104150

105151
await writeFile(
106152
"README.md",
@@ -111,6 +157,7 @@ await writeFile(
111157

112158
await writeFile("index.js", indexJs);
113159

114-
await exec(`${answers.npm_executable.toLowerCase()} install`);
160+
await exec(`${answers.npm_executable} install`);
161+
await exec(`${answers.npm_executable} run prepare`);
115162

116163
console.log("Utility created!");

packages/create-utility/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hkamran/create-utility",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"license": "AGPL-3.0-or-later",
55
"description": "Create a utility",
66
"main": "index.js",

0 commit comments

Comments
 (0)