jiek 0.2.1 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/init.esm.js +271 -0
- package/dist/commands/init.esm.js.map +1 -0
- package/dist/commands/init.esm.min.js +2 -0
- package/dist/commands/init.esm.min.js.map +1 -0
- package/dist/index.esm.js +3 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/index.iife.js +427 -63
- package/dist/index.iife.js.map +1 -1
- package/dist/index.iife.min.js +1 -1
- package/dist/index.iife.min.js.map +1 -1
- package/dist/index.umd.js +429 -65
- package/dist/index.umd.js.map +1 -1
- package/dist/index.umd.min.js +1 -1
- package/dist/index.umd.min.js.map +1 -1
- package/dist/utils/filterSupport.esm.js +4 -15
- package/dist/utils/filterSupport.esm.js.map +1 -1
- package/dist/utils/filterSupport.esm.min.js +1 -1
- package/dist/utils/filterSupport.esm.min.js.map +1 -1
- package/dist/utils/getRoot.esm.js +14 -0
- package/dist/utils/getRoot.esm.js.map +1 -0
- package/dist/utils/getRoot.esm.min.js +2 -0
- package/dist/utils/getRoot.esm.min.js.map +1 -0
- package/dist/utils/getWD.esm.js +25 -0
- package/dist/utils/getWD.esm.js.map +1 -0
- package/dist/utils/getWD.esm.min.js +2 -0
- package/dist/utils/getWD.esm.min.js.map +1 -0
- package/dist/utils/loadConfig.esm.js +94 -0
- package/dist/utils/loadConfig.esm.js.map +1 -0
- package/dist/utils/loadConfig.esm.min.js +2 -0
- package/dist/utils/loadConfig.esm.min.js.map +1 -0
- package/package.json +7 -2
@@ -0,0 +1,271 @@
|
|
1
|
+
import fs from 'node:fs';
|
2
|
+
import path from 'node:path';
|
3
|
+
import { program } from 'commander';
|
4
|
+
import detectIndent from 'detect-indent';
|
5
|
+
import inquirer from 'inquirer';
|
6
|
+
import { applyEdits, modify } from 'jsonc-parser';
|
7
|
+
import { isMatch } from 'micromatch';
|
8
|
+
import { getWD } from '../utils/getWD.esm.js';
|
9
|
+
import { loadConfig } from '../utils/loadConfig.esm.js';
|
10
|
+
|
11
|
+
const PACKAGE_JSON_TEMPLATE = `{
|
12
|
+
"name": "",
|
13
|
+
"version": "0.0.1",
|
14
|
+
"description": "",
|
15
|
+
"license": "",
|
16
|
+
"author": "",
|
17
|
+
"files": ["dist"],
|
18
|
+
"exports": {
|
19
|
+
".": "./src/index.ts"
|
20
|
+
},
|
21
|
+
"scripts": {
|
22
|
+
},
|
23
|
+
"homepage": "",
|
24
|
+
"repository": "",
|
25
|
+
"bugs": ""
|
26
|
+
}`.trimStart();
|
27
|
+
const README_TEMPLATE = `# $name
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
\`\`\`bash
|
32
|
+
npm install $name
|
33
|
+
# or
|
34
|
+
pnpm install $name
|
35
|
+
# or
|
36
|
+
yarn add $name
|
37
|
+
\`\`\`
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
$license
|
45
|
+
`.trimStart();
|
46
|
+
function getTemplateStr(wd, template) {
|
47
|
+
let templateString = template ?? PACKAGE_JSON_TEMPLATE;
|
48
|
+
let isTemplateFile = false;
|
49
|
+
try {
|
50
|
+
if (template)
|
51
|
+
JSON.parse(template);
|
52
|
+
} catch (e) {
|
53
|
+
isTemplateFile = true;
|
54
|
+
}
|
55
|
+
if (isTemplateFile) {
|
56
|
+
const templatePath = path.resolve(wd, template);
|
57
|
+
templateString = fs.readFileSync(templatePath, "utf-8");
|
58
|
+
}
|
59
|
+
return templateString;
|
60
|
+
}
|
61
|
+
const wdCache = /* @__PURE__ */ new Map();
|
62
|
+
function getWDPackageJSONFiled(wd, field) {
|
63
|
+
if (wdCache.has(wd)) {
|
64
|
+
return wdCache.get(wd)[field];
|
65
|
+
}
|
66
|
+
const packageJSONPath = path.resolve(wd, "package.json");
|
67
|
+
const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, "utf-8"));
|
68
|
+
wdCache.set(wd, packageJSON);
|
69
|
+
return packageJSON[field];
|
70
|
+
}
|
71
|
+
async function getName(named, name, {
|
72
|
+
wd,
|
73
|
+
cwd,
|
74
|
+
workspaceName
|
75
|
+
}) {
|
76
|
+
const relativePath = cwd.replace(`${wd}/`, "");
|
77
|
+
let basename = path.basename(cwd);
|
78
|
+
if (typeof named === "function") {
|
79
|
+
return named(name, {
|
80
|
+
full: wd,
|
81
|
+
relative: cwd
|
82
|
+
});
|
83
|
+
}
|
84
|
+
let isParentMatched = false;
|
85
|
+
let matchedKey;
|
86
|
+
let matchedRule;
|
87
|
+
if (typeof named === "object") {
|
88
|
+
const isWD = cwd === wd;
|
89
|
+
if (isWD) {
|
90
|
+
const { rule } = await inquirer.prompt({
|
91
|
+
type: "list",
|
92
|
+
name: "rule",
|
93
|
+
message: "choose a rule",
|
94
|
+
default: "default",
|
95
|
+
choices: ["default"].concat(Object.keys(named))
|
96
|
+
});
|
97
|
+
if (rule !== "default") {
|
98
|
+
matchedKey = rule;
|
99
|
+
matchedRule = named[rule];
|
100
|
+
}
|
101
|
+
} else
|
102
|
+
for (const [key, value] of Object.entries(named)) {
|
103
|
+
if (isMatch(relativePath, key)) {
|
104
|
+
matchedKey = key;
|
105
|
+
matchedRule = value;
|
106
|
+
break;
|
107
|
+
}
|
108
|
+
if (isMatch(`${relativePath}/jiek_ignore_dont_use_same_file_name`, key)) {
|
109
|
+
isParentMatched = true;
|
110
|
+
matchedKey = key;
|
111
|
+
matchedRule = value;
|
112
|
+
break;
|
113
|
+
}
|
114
|
+
}
|
115
|
+
}
|
116
|
+
if (!matchedRule) {
|
117
|
+
matchedKey = "packages/*";
|
118
|
+
matchedRule = `@${workspaceName}/$basename`;
|
119
|
+
}
|
120
|
+
if (!matchedRule)
|
121
|
+
throw new Error("no matched rule");
|
122
|
+
if (!name && isParentMatched) {
|
123
|
+
basename = await inquirer.prompt({
|
124
|
+
type: "input",
|
125
|
+
name: "name",
|
126
|
+
message: `the matched rule is \`${String(matchedRule)}\`, please input the basename
|
127
|
+
`
|
128
|
+
}).then(({ name: name2 }) => name2);
|
129
|
+
}
|
130
|
+
if (typeof matchedRule === "function") {
|
131
|
+
return matchedRule(name, {
|
132
|
+
full: wd,
|
133
|
+
relative: cwd,
|
134
|
+
basename
|
135
|
+
});
|
136
|
+
}
|
137
|
+
if (typeof matchedRule === "string") {
|
138
|
+
const dirName = name ?? basename;
|
139
|
+
return [
|
140
|
+
matchedRule.replace(/\$basename/g, dirName),
|
141
|
+
matchedKey?.replace(/\/\*$/g, `/${dirName}`)
|
142
|
+
];
|
143
|
+
}
|
144
|
+
throw new Error("no matched rule");
|
145
|
+
}
|
146
|
+
program.command("init [name]").option("-t, --template <template>", "the package.json template file path or file content").action(async () => {
|
147
|
+
const [, name] = program.args;
|
148
|
+
const cwd = process.cwd();
|
149
|
+
const { init = {} } = loadConfig() ?? {};
|
150
|
+
const { wd } = getWD();
|
151
|
+
const workspaceName = path.basename(wd);
|
152
|
+
const {
|
153
|
+
named,
|
154
|
+
template,
|
155
|
+
bug = {},
|
156
|
+
readme: _readme = README_TEMPLATE,
|
157
|
+
readmeTemplate
|
158
|
+
} = init;
|
159
|
+
const resolvedBug = {
|
160
|
+
template: "bug_report.yml",
|
161
|
+
labels: ["bug"],
|
162
|
+
...bug
|
163
|
+
};
|
164
|
+
let readme = _readme;
|
165
|
+
if (readmeTemplate) {
|
166
|
+
const readmeTemplatePath = path.resolve(wd, readmeTemplate);
|
167
|
+
readme = fs.readFileSync(readmeTemplatePath, "utf-8");
|
168
|
+
}
|
169
|
+
const templateString = getTemplateStr(wd, template);
|
170
|
+
const { indent = " " } = detectIndent(templateString);
|
171
|
+
const formattingOptions = {
|
172
|
+
tabSize: indent.length,
|
173
|
+
insertSpaces: true
|
174
|
+
};
|
175
|
+
const passFields = [
|
176
|
+
"license",
|
177
|
+
"author"
|
178
|
+
];
|
179
|
+
let newJSONString = templateString;
|
180
|
+
for (const field of passFields) {
|
181
|
+
newJSONString = applyEdits(newJSONString, modify(
|
182
|
+
newJSONString,
|
183
|
+
[field],
|
184
|
+
getWDPackageJSONFiled(wd, field),
|
185
|
+
{ formattingOptions }
|
186
|
+
));
|
187
|
+
}
|
188
|
+
let [pkgName, pkgDir] = await getName(named, name, {
|
189
|
+
wd,
|
190
|
+
cwd,
|
191
|
+
workspaceName
|
192
|
+
});
|
193
|
+
if (!pkgDir) {
|
194
|
+
const { dir } = await inquirer.prompt({
|
195
|
+
type: "input",
|
196
|
+
name: "dir",
|
197
|
+
message: "package directory",
|
198
|
+
default: name
|
199
|
+
});
|
200
|
+
pkgDir = dir;
|
201
|
+
}
|
202
|
+
if (!pkgName) {
|
203
|
+
const { name: inputName } = await inquirer.prompt({
|
204
|
+
type: "input",
|
205
|
+
name: "name",
|
206
|
+
message: "package name",
|
207
|
+
default: name
|
208
|
+
});
|
209
|
+
pkgName = inputName;
|
210
|
+
}
|
211
|
+
newJSONString = applyEdits(newJSONString, modify(newJSONString, ["name"], pkgName, { formattingOptions }));
|
212
|
+
let pkgRepo = getWDPackageJSONFiled(wd, "repository");
|
213
|
+
if (typeof pkgRepo === "string") {
|
214
|
+
pkgRepo = {
|
215
|
+
type: "git",
|
216
|
+
url: pkgRepo,
|
217
|
+
directory: pkgDir
|
218
|
+
};
|
219
|
+
}
|
220
|
+
newJSONString = applyEdits(newJSONString, modify(
|
221
|
+
newJSONString,
|
222
|
+
["repository"],
|
223
|
+
pkgRepo,
|
224
|
+
{ formattingOptions }
|
225
|
+
));
|
226
|
+
const homepage = `${pkgRepo?.url}/blob/master/${pkgDir}/README.md`;
|
227
|
+
newJSONString = applyEdits(newJSONString, modify(
|
228
|
+
newJSONString,
|
229
|
+
["homepage"],
|
230
|
+
homepage,
|
231
|
+
{ formattingOptions }
|
232
|
+
));
|
233
|
+
let labels = resolvedBug.labels;
|
234
|
+
if (typeof labels === "function")
|
235
|
+
labels = labels({
|
236
|
+
name: pkgName,
|
237
|
+
dir: pkgDir
|
238
|
+
});
|
239
|
+
labels.push(`scope:${pkgName}`);
|
240
|
+
const bugs = `${pkgRepo?.url}/issues/new?template=${resolvedBug.template}&labels=${labels.join(",")}`;
|
241
|
+
newJSONString = applyEdits(newJSONString, modify(
|
242
|
+
newJSONString,
|
243
|
+
["bugs"],
|
244
|
+
bugs,
|
245
|
+
{ formattingOptions }
|
246
|
+
));
|
247
|
+
function pkgDirTo(to) {
|
248
|
+
if (!pkgDir)
|
249
|
+
throw new Error("pkgDir is not defined");
|
250
|
+
return path.resolve(pkgDir, to);
|
251
|
+
}
|
252
|
+
if (!fs.existsSync(pkgDir))
|
253
|
+
fs.mkdirSync(pkgDir);
|
254
|
+
const pkgJSONFilePath = pkgDirTo("package.json");
|
255
|
+
if (fs.existsSync(pkgJSONFilePath)) {
|
256
|
+
throw new Error("package.json already exists");
|
257
|
+
}
|
258
|
+
fs.writeFileSync(pkgJSONFilePath, newJSONString);
|
259
|
+
console.log(newJSONString, "written to", pkgJSONFilePath);
|
260
|
+
const license = getWDPackageJSONFiled(wd, "license");
|
261
|
+
const readmeFilePath = pkgDirTo("README.md");
|
262
|
+
if (typeof readme === "function") {
|
263
|
+
readme = readme({
|
264
|
+
dir: pkgDir,
|
265
|
+
packageJson: JSON.parse(newJSONString)
|
266
|
+
});
|
267
|
+
}
|
268
|
+
const readmeContent = readme.replace(/\$name/g, pkgName).replace(/\$license/g, license);
|
269
|
+
fs.writeFileSync(readmeFilePath, readmeContent);
|
270
|
+
});
|
271
|
+
//# sourceMappingURL=init.esm.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"init.esm.js","sources":["../../src/commands/init.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\n\nimport { program } from 'commander'\nimport detectIndent from 'detect-indent'\nimport inquirer from 'inquirer'\nimport { applyEdits, modify } from 'jsonc-parser'\nimport { isMatch } from 'micromatch'\n\nimport type { Config, InitNamed } from '../base'\nimport { getWD } from '../utils/getWD'\nimport { loadConfig } from '../utils/loadConfig'\n\nconst PACKAGE_JSON_TEMPLATE = `{\n \"name\": \"\",\n \"version\": \"0.0.1\",\n \"description\": \"\",\n \"license\": \"\",\n \"author\": \"\",\n \"files\": [\"dist\"],\n \"exports\": {\n \".\": \"./src/index.ts\"\n },\n \"scripts\": {\n },\n \"homepage\": \"\",\n \"repository\": \"\",\n \"bugs\": \"\"\n}`.trimStart()\nconst README_TEMPLATE = `# $name\n\n## Installation\n\n\\`\\`\\`bash\nnpm install $name\n# or\npnpm install $name\n# or\nyarn add $name\n\\`\\`\\`\n\n## Usage\n\n\n## License\n\n$license\n`.trimStart()\n\nfunction getTemplateStr(wd: string, template: string | undefined) {\n let templateString = template ?? PACKAGE_JSON_TEMPLATE\n let isTemplateFile = false\n try {\n if (template) JSON.parse(template)\n } catch (e) {\n isTemplateFile = true\n }\n if (isTemplateFile) {\n const templatePath = path.resolve(wd, template!)\n templateString = fs.readFileSync(templatePath, 'utf-8')\n }\n return templateString\n}\nconst wdCache = new Map<string, Record<string, any>>()\nfunction getWDPackageJSONFiled(wd: string, field: string) {\n if (wdCache.has(wd)) {\n return wdCache.get(wd)![field]\n }\n const packageJSONPath = path.resolve(wd, 'package.json')\n const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'))\n wdCache.set(wd, packageJSON)\n return packageJSON[field]\n}\nasync function getName(\n named: InitNamed | undefined,\n name: string,\n {\n wd, cwd,\n workspaceName\n }: {\n wd: string\n cwd: string\n workspaceName: string\n }\n): Promise<[name?: string, path?: string]> {\n const relativePath = cwd.replace(`${wd}/`, '')\n let basename = path.basename(cwd)\n\n if (typeof named === 'function') {\n return named(name, {\n full: wd,\n relative: cwd\n })\n }\n\n let isParentMatched = false\n let matchedKey: string | undefined\n let matchedRule: NonNullable<typeof named>[string] | undefined\n if (typeof named === 'object') {\n const isWD = cwd === wd\n if (isWD) {\n const { rule } = await inquirer.prompt<{ rule: string }>({\n type: 'list',\n name: 'rule',\n message: 'choose a rule',\n default: 'default',\n choices: ['default'].concat(Object.keys(named))\n })\n if (rule !== 'default') {\n matchedKey = rule\n matchedRule = named[rule]\n }\n } else for (const [key, value] of Object.entries(named)) {\n if (isMatch(relativePath, key)) {\n matchedKey = key\n matchedRule = value\n break\n }\n if (isMatch(`${relativePath}/jiek_ignore_dont_use_same_file_name`, key)) {\n isParentMatched = true\n matchedKey = key\n matchedRule = value\n break\n }\n }\n }\n if (!matchedRule) {\n matchedKey = 'packages/*'\n matchedRule = `@${workspaceName}/$basename`\n }\n if (!matchedRule)\n throw new Error('no matched rule')\n if (!name && isParentMatched) {\n basename = await inquirer.prompt<{ name: string }>({\n type: 'input',\n name: 'name',\n message: `the matched rule is \\`${String(matchedRule)}\\`, please input the basename\\n`\n }).then(({ name }) => name)\n }\n\n if (typeof matchedRule === 'function') {\n return matchedRule(name, {\n full: wd,\n relative: cwd,\n basename: basename\n })\n }\n if (typeof matchedRule === 'string') {\n const dirName = name ?? basename\n return [\n matchedRule.replace(/\\$basename/g, dirName),\n matchedKey?.replace(/\\/\\*$/g, `/${dirName}`)\n ]\n }\n throw new Error('no matched rule')\n}\n\nprogram\n .command('init [name]')\n .option('-t, --template <template>', 'the package.json template file path or file content')\n .action(async () => {\n const [, name] = program.args\n const cwd = process.cwd()\n const { init = {} }: Config = loadConfig() ?? {}\n const { wd } = getWD()\n const workspaceName = path.basename(wd)\n\n const {\n named,\n template,\n bug = {},\n readme: _readme = README_TEMPLATE,\n readmeTemplate\n } = init\n const resolvedBug = {\n template: 'bug_report.yml',\n labels: ['bug'],\n ...bug\n }\n let readme = _readme\n if (readmeTemplate) {\n const readmeTemplatePath = path.resolve(wd, readmeTemplate)\n readme = fs.readFileSync(readmeTemplatePath, 'utf-8')\n }\n\n const templateString = getTemplateStr(wd, template)\n // TODO detectIndent by editorconfig\n const { indent = ' ' } = detectIndent(templateString)\n const formattingOptions = {\n tabSize: indent.length,\n insertSpaces: true\n }\n const passFields = [\n 'license', 'author'\n ]\n let newJSONString = templateString\n for (const field of passFields) {\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, [field], getWDPackageJSONFiled(wd, field), { formattingOptions }\n ))\n }\n let [pkgName, pkgDir] = await getName(named, name, {\n wd, cwd,\n workspaceName\n })\n if (!pkgDir) {\n const { dir } = await inquirer.prompt<{ dir: string }>({\n type: 'input',\n name: 'dir',\n message: 'package directory',\n default: name\n })\n pkgDir = dir\n }\n if (!pkgName) {\n const { name: inputName } = await inquirer.prompt<{\n name: string\n }>({\n type: 'input',\n name: 'name',\n message: 'package name',\n default: name\n })\n pkgName = inputName\n }\n newJSONString = applyEdits(newJSONString, modify(newJSONString, ['name'], pkgName, { formattingOptions }))\n\n let pkgRepo = getWDPackageJSONFiled(wd, 'repository')\n if (typeof pkgRepo === 'string') {\n pkgRepo = {\n type: 'git',\n url: pkgRepo,\n directory: pkgDir\n }\n }\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, ['repository'], pkgRepo, { formattingOptions }\n ))\n const homepage = `${pkgRepo?.url}/blob/master/${pkgDir}/README.md`\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, ['homepage'], homepage, { formattingOptions }\n ))\n let labels = resolvedBug.labels\n if (typeof labels === 'function') labels = labels({\n name: pkgName, dir: pkgDir\n })\n labels.push(`scope:${pkgName}`)\n const bugs = `${pkgRepo?.url}/issues/new?template=${\n resolvedBug.template\n }&labels=${\n labels.join(',')\n }`\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, ['bugs'], bugs, { formattingOptions }\n ))\n\n function pkgDirTo(to: string) {\n if (!pkgDir) throw new Error('pkgDir is not defined')\n\n return path.resolve(pkgDir, to)\n }\n if (!fs.existsSync(pkgDir)) fs.mkdirSync(pkgDir)\n const pkgJSONFilePath = pkgDirTo('package.json')\n if (fs.existsSync(pkgJSONFilePath)) {\n throw new Error('package.json already exists')\n }\n fs.writeFileSync(pkgJSONFilePath, newJSONString)\n console.log(newJSONString, 'written to', pkgJSONFilePath)\n\n const license = getWDPackageJSONFiled(wd, 'license')\n const readmeFilePath = pkgDirTo('README.md')\n if (typeof readme === 'function') {\n readme = readme({\n dir: pkgDir,\n packageJson: JSON.parse(newJSONString)\n })\n }\n const readmeContent = readme\n .replace(/\\$name/g, pkgName)\n .replace(/\\$license/g, license)\n fs.writeFileSync(readmeFilePath, readmeContent)\n })\n"],"names":["name"],"mappings":";;;;;;;;;;AAaA,MAAM,qBAAwB,GAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA,CAAA,CAe3B,SAAU,EAAA,CAAA;AACb,MAAM,eAAkB,GAAA,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,CAAA,CAkBtB,SAAU,EAAA,CAAA;AAEZ,SAAS,cAAA,CAAe,IAAY,QAA8B,EAAA;AAChE,EAAA,IAAI,iBAAiB,QAAY,IAAA,qBAAA,CAAA;AACjC,EAAA,IAAI,cAAiB,GAAA,KAAA,CAAA;AACrB,EAAI,IAAA;AACF,IAAI,IAAA,QAAA;AAAU,MAAA,IAAA,CAAK,MAAM,QAAQ,CAAA,CAAA;AAAA,WAC1B,CAAG,EAAA;AACV,IAAiB,cAAA,GAAA,IAAA,CAAA;AAAA,GACnB;AACA,EAAA,IAAI,cAAgB,EAAA;AAClB,IAAA,MAAM,YAAe,GAAA,IAAA,CAAK,OAAQ,CAAA,EAAA,EAAI,QAAS,CAAA,CAAA;AAC/C,IAAiB,cAAA,GAAA,EAAA,CAAG,YAAa,CAAA,YAAA,EAAc,OAAO,CAAA,CAAA;AAAA,GACxD;AACA,EAAO,OAAA,cAAA,CAAA;AACT,CAAA;AACA,MAAM,OAAA,uBAAc,GAAiC,EAAA,CAAA;AACrD,SAAS,qBAAA,CAAsB,IAAY,KAAe,EAAA;AACxD,EAAI,IAAA,OAAA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,CAAA,CAAA;AAC/C,EAAI,IAAA,EAAA,CAAG,UAAW,CAAA,eAAe,CAAG,EAAA;AAClC,IAAM,MAAA,IAAI,MAAM,6BAA6B,CAAA,CAAA;AAAA,GAC/C;AACA,EAAG,EAAA,CAAA,aAAA,CAAc,iBAAiB,aAAa,CAAA,CAAA;AAC/C,EAAQ,OAAA,CAAA,GAAA,CAAI,aAAe,EAAA,YAAA,EAAc,eAAe,CAAA,CAAA;AAExD,EAAM,MAAA,OAAA,GAAU,qBAAsB,CAAA,EAAA,EAAI,SAAS,CAAA,CAAA;AACnD,EAAM,MAAA,cAAA,GAAiB,SAAS,WAAW,CAAA,CAAA;AAC3C,EAAI,IAAA,OAAO,WAAW,UAAY,EAAA;AAChC,IAAA,MAAA,GAAS,MAAO,CAAA;AAAA,MACd,GAAK,EAAA,MAAA;AAAA,MACL,WAAA,EAAa,IAAK,CAAA,KAAA,CAAM,aAAa,CAAA;AAAA,KACtC,CAAA,CAAA;AAAA,GACH;AACA,EAAM,MAAA,aAAA,GAAgB,OACnB,OAAQ,CAAA,SAAA,EAAW,OAAO,CAC1B,CAAA,OAAA,CAAQ,cAAc,OAAO,CAAA,CAAA;AAChC,EAAG,EAAA,CAAA,aAAA,CAAc,gBAAgB,aAAa,CAAA,CAAA;AAChD,CAAC,CAAA"}
|
@@ -0,0 +1,2 @@
|
|
1
|
+
import e from"node:fs";import t from"node:path";import{program as n}from"commander";import a from"detect-indent";import r from"inquirer";import{applyEdits as o,modify as i}from"jsonc-parser";import{isMatch as s}from"micromatch";import{getWD as m}from"../utils/getWD.esm.min.js";import{loadConfig as c}from"../utils/loadConfig.esm.min.js";const p='{\n "name": "",\n "version": "0.0.1",\n "description": "",\n "license": "",\n "author": "",\n "files": ["dist"],\n "exports": {\n ".": "./src/index.ts"\n },\n "scripts": {\n },\n "homepage": "",\n "repository": "",\n "bugs": ""\n}'.trimStart(),l="# $name\n\n## Installation\n\n```bash\nnpm install $name\n# or\npnpm install $name\n# or\nyarn add $name\n```\n\n## Usage\n\n\n## License\n\n$license\n".trimStart();const f=new Map;function u(n,a){if(f.has(n))return f.get(n)[a];const r=t.resolve(n,"package.json"),o=JSON.parse(e.readFileSync(r,"utf-8"));return f.set(n,o),o[a]}n.command("init [name]").option("-t, --template <template>","the package.json template file path or file content").action((async()=>{const[,f]=n.args,d=process.cwd(),{init:g={}}=c()??{},{wd:y}=m(),w=t.basename(y),{named:h,template:b,bug:$={},readme:k=l,readmeTemplate:S}=g,j={template:"bug_report.yml",labels:["bug"],...$};let O=k;if(S){const n=t.resolve(y,S);O=e.readFileSync(n,"utf-8")}const E=function(n,a){let r=a??p,o=!1;try{a&&JSON.parse(a)}catch(e){o=!0}if(o){const o=t.resolve(n,a);r=e.readFileSync(o,"utf-8")}return r}(y,b),{indent:v=" "}=a(E),_={tabSize:v.length,insertSpaces:!0},x=["license","author"];let F=E;for(const e of x)F=o(F,i(F,[e],u(y,e),{formattingOptions:_}));let[N,D]=await async function(e,n,{wd:a,cwd:o,workspaceName:i}){const m=o.replace(`${a}/`,"");let c=t.basename(o);if("function"==typeof e)return e(n,{full:a,relative:o});let p,l,f=!1;if("object"==typeof e)if(o===a){const{rule:t}=await r.prompt({type:"list",name:"rule",message:"choose a rule",default:"default",choices:["default"].concat(Object.keys(e))});"default"!==t&&(p=t,l=e[t])}else for(const[t,n]of Object.entries(e)){if(s(m,t)){p=t,l=n;break}if(s(`${m}/jiek_ignore_dont_use_same_file_name`,t)){f=!0,p=t,l=n;break}}if(l||(p="packages/*",l=`@${i}/$basename`),!l)throw new Error("no matched rule");if(!n&&f&&(c=await r.prompt({type:"input",name:"name",message:`the matched rule is \`${String(l)}\`, please input the basename\n`}).then((({name:e})=>e))),"function"==typeof l)return l(n,{full:a,relative:o,basename:c});if("string"==typeof l){const e=n??c;return[l.replace(/\$basename/g,e),p?.replace(/\/\*$/g,`/${e}`)]}throw new Error("no matched rule")}(h,f,{wd:y,cwd:d,workspaceName:w});if(!D){const{dir:e}=await r.prompt({type:"input",name:"dir",message:"package directory",default:f});D=e}if(!N){const{name:e}=await r.prompt({type:"input",name:"name",message:"package name",default:f});N=e}F=o(F,i(F,["name"],N,{formattingOptions:_}));let J=u(y,"repository");"string"==typeof J&&(J={type:"git",url:J,directory:D}),F=o(F,i(F,["repository"],J,{formattingOptions:_}));F=o(F,i(F,["homepage"],`${J?.url}/blob/master/${D}/README.md`,{formattingOptions:_}));let M=j.labels;"function"==typeof M&&(M=M({name:N,dir:D})),M.push(`scope:${N}`);const A=`${J?.url}/issues/new?template=${j.template}&labels=${M.join(",")}`;function R(e){if(!D)throw new Error("pkgDir is not defined");return t.resolve(D,e)}F=o(F,i(F,["bugs"],A,{formattingOptions:_})),e.existsSync(D)||e.mkdirSync(D);const q=R("package.json");if(e.existsSync(q))throw new Error("package.json already exists");e.writeFileSync(q,F),console.log(F,"written to",q);const z=u(y,"license"),C=R("README.md");"function"==typeof O&&(O=O({dir:D,packageJson:JSON.parse(F)}));const I=O.replace(/\$name/g,N).replace(/\$license/g,z);e.writeFileSync(C,I)}));
|
2
|
+
//# sourceMappingURL=init.esm.min.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"init.esm.min.js","sources":["../../src/commands/init.ts"],"sourcesContent":["import fs from 'node:fs'\nimport path from 'node:path'\n\nimport { program } from 'commander'\nimport detectIndent from 'detect-indent'\nimport inquirer from 'inquirer'\nimport { applyEdits, modify } from 'jsonc-parser'\nimport { isMatch } from 'micromatch'\n\nimport type { Config, InitNamed } from '../base'\nimport { getWD } from '../utils/getWD'\nimport { loadConfig } from '../utils/loadConfig'\n\nconst PACKAGE_JSON_TEMPLATE = `{\n \"name\": \"\",\n \"version\": \"0.0.1\",\n \"description\": \"\",\n \"license\": \"\",\n \"author\": \"\",\n \"files\": [\"dist\"],\n \"exports\": {\n \".\": \"./src/index.ts\"\n },\n \"scripts\": {\n },\n \"homepage\": \"\",\n \"repository\": \"\",\n \"bugs\": \"\"\n}`.trimStart()\nconst README_TEMPLATE = `# $name\n\n## Installation\n\n\\`\\`\\`bash\nnpm install $name\n# or\npnpm install $name\n# or\nyarn add $name\n\\`\\`\\`\n\n## Usage\n\n\n## License\n\n$license\n`.trimStart()\n\nfunction getTemplateStr(wd: string, template: string | undefined) {\n let templateString = template ?? PACKAGE_JSON_TEMPLATE\n let isTemplateFile = false\n try {\n if (template) JSON.parse(template)\n } catch (e) {\n isTemplateFile = true\n }\n if (isTemplateFile) {\n const templatePath = path.resolve(wd, template!)\n templateString = fs.readFileSync(templatePath, 'utf-8')\n }\n return templateString\n}\nconst wdCache = new Map<string, Record<string, any>>()\nfunction getWDPackageJSONFiled(wd: string, field: string) {\n if (wdCache.has(wd)) {\n return wdCache.get(wd)![field]\n }\n const packageJSONPath = path.resolve(wd, 'package.json')\n const packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'))\n wdCache.set(wd, packageJSON)\n return packageJSON[field]\n}\nasync function getName(\n named: InitNamed | undefined,\n name: string,\n {\n wd, cwd,\n workspaceName\n }: {\n wd: string\n cwd: string\n workspaceName: string\n }\n): Promise<[name?: string, path?: string]> {\n const relativePath = cwd.replace(`${wd}/`, '')\n let basename = path.basename(cwd)\n\n if (typeof named === 'function') {\n return named(name, {\n full: wd,\n relative: cwd\n })\n }\n\n let isParentMatched = false\n let matchedKey: string | undefined\n let matchedRule: NonNullable<typeof named>[string] | undefined\n if (typeof named === 'object') {\n const isWD = cwd === wd\n if (isWD) {\n const { rule } = await inquirer.prompt<{ rule: string }>({\n type: 'list',\n name: 'rule',\n message: 'choose a rule',\n default: 'default',\n choices: ['default'].concat(Object.keys(named))\n })\n if (rule !== 'default') {\n matchedKey = rule\n matchedRule = named[rule]\n }\n } else for (const [key, value] of Object.entries(named)) {\n if (isMatch(relativePath, key)) {\n matchedKey = key\n matchedRule = value\n break\n }\n if (isMatch(`${relativePath}/jiek_ignore_dont_use_same_file_name`, key)) {\n isParentMatched = true\n matchedKey = key\n matchedRule = value\n break\n }\n }\n }\n if (!matchedRule) {\n matchedKey = 'packages/*'\n matchedRule = `@${workspaceName}/$basename`\n }\n if (!matchedRule)\n throw new Error('no matched rule')\n if (!name && isParentMatched) {\n basename = await inquirer.prompt<{ name: string }>({\n type: 'input',\n name: 'name',\n message: `the matched rule is \\`${String(matchedRule)}\\`, please input the basename\\n`\n }).then(({ name }) => name)\n }\n\n if (typeof matchedRule === 'function') {\n return matchedRule(name, {\n full: wd,\n relative: cwd,\n basename: basename\n })\n }\n if (typeof matchedRule === 'string') {\n const dirName = name ?? basename\n return [\n matchedRule.replace(/\\$basename/g, dirName),\n matchedKey?.replace(/\\/\\*$/g, `/${dirName}`)\n ]\n }\n throw new Error('no matched rule')\n}\n\nprogram\n .command('init [name]')\n .option('-t, --template <template>', 'the package.json template file path or file content')\n .action(async () => {\n const [, name] = program.args\n const cwd = process.cwd()\n const { init = {} }: Config = loadConfig() ?? {}\n const { wd } = getWD()\n const workspaceName = path.basename(wd)\n\n const {\n named,\n template,\n bug = {},\n readme: _readme = README_TEMPLATE,\n readmeTemplate\n } = init\n const resolvedBug = {\n template: 'bug_report.yml',\n labels: ['bug'],\n ...bug\n }\n let readme = _readme\n if (readmeTemplate) {\n const readmeTemplatePath = path.resolve(wd, readmeTemplate)\n readme = fs.readFileSync(readmeTemplatePath, 'utf-8')\n }\n\n const templateString = getTemplateStr(wd, template)\n // TODO detectIndent by editorconfig\n const { indent = ' ' } = detectIndent(templateString)\n const formattingOptions = {\n tabSize: indent.length,\n insertSpaces: true\n }\n const passFields = [\n 'license', 'author'\n ]\n let newJSONString = templateString\n for (const field of passFields) {\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, [field], getWDPackageJSONFiled(wd, field), { formattingOptions }\n ))\n }\n let [pkgName, pkgDir] = await getName(named, name, {\n wd, cwd,\n workspaceName\n })\n if (!pkgDir) {\n const { dir } = await inquirer.prompt<{ dir: string }>({\n type: 'input',\n name: 'dir',\n message: 'package directory',\n default: name\n })\n pkgDir = dir\n }\n if (!pkgName) {\n const { name: inputName } = await inquirer.prompt<{\n name: string\n }>({\n type: 'input',\n name: 'name',\n message: 'package name',\n default: name\n })\n pkgName = inputName\n }\n newJSONString = applyEdits(newJSONString, modify(newJSONString, ['name'], pkgName, { formattingOptions }))\n\n let pkgRepo = getWDPackageJSONFiled(wd, 'repository')\n if (typeof pkgRepo === 'string') {\n pkgRepo = {\n type: 'git',\n url: pkgRepo,\n directory: pkgDir\n }\n }\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, ['repository'], pkgRepo, { formattingOptions }\n ))\n const homepage = `${pkgRepo?.url}/blob/master/${pkgDir}/README.md`\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, ['homepage'], homepage, { formattingOptions }\n ))\n let labels = resolvedBug.labels\n if (typeof labels === 'function') labels = labels({\n name: pkgName, dir: pkgDir\n })\n labels.push(`scope:${pkgName}`)\n const bugs = `${pkgRepo?.url}/issues/new?template=${\n resolvedBug.template\n }&labels=${\n labels.join(',')\n }`\n newJSONString = applyEdits(newJSONString, modify(\n newJSONString, ['bugs'], bugs, { formattingOptions }\n ))\n\n function pkgDirTo(to: string) {\n if (!pkgDir) throw new Error('pkgDir is not defined')\n\n return path.resolve(pkgDir, to)\n }\n if (!fs.existsSync(pkgDir)) fs.mkdirSync(pkgDir)\n const pkgJSONFilePath = pkgDirTo('package.json')\n if (fs.existsSync(pkgJSONFilePath)) {\n throw new Error('package.json already exists')\n }\n fs.writeFileSync(pkgJSONFilePath, newJSONString)\n console.log(newJSONString, 'written to', pkgJSONFilePath)\n\n const license = getWDPackageJSONFiled(wd, 'license')\n const readmeFilePath = pkgDirTo('README.md')\n if (typeof readme === 'function') {\n readme = readme({\n dir: pkgDir,\n packageJson: JSON.parse(newJSONString)\n })\n }\n const readmeContent = readme\n .replace(/\\$name/g, pkgName)\n .replace(/\\$license/g, license)\n fs.writeFileSync(readmeFilePath, readmeContent)\n })\n"],"names":["PACKAGE_JSON_TEMPLATE","trimStart","README_TEMPLATE","wdCache","Map","getWDPackageJSONFiled","wd","field","has","get","packageJSONPath","path","resolve","packageJSON","JSON","parse","fs","readFileSync","set","program","command","option","action","async","name","args","cwd","process","init","loadConfig","getWD","workspaceName","basename","named","template","bug","readme","_readme","readmeTemplate","resolvedBug","labels","readmeTemplatePath","templateString","isTemplateFile","e","templatePath","getTemplateStr","indent","detectIndent","formattingOptions","tabSize","length","insertSpaces","passFields","newJSONString","applyEdits","modify","pkgName","pkgDir","relativePath","replace","full","relative","matchedKey","matchedRule","isParentMatched","rule","inquirer","prompt","type","message","default","choices","concat","Object","keys","key","value","entries","isMatch","Error","String","then","dirName","getName","dir","inputName","pkgRepo","url","directory","push","bugs","join","pkgDirTo","to","existsSync","mkdirSync","pkgJSONFilePath","writeFileSync","console","log","license","readmeFilePath","packageJson","readmeContent"],"mappings":"kVAaA,MAAMA,EAAwB,yPAe3BC,YACGC,EAAkB,0JAkBtBD,YAgBF,MAAME,MAAcC,IACpB,SAASC,EAAsBC,EAAYC,GACrC,GAAAJ,EAAQK,IAAIF,GACd,OAAOH,EAAQM,IAAIH,GAAKC,GAE1B,MAAMG,EAAkBC,EAAKC,QAAQN,EAAI,gBACnCO,EAAcC,KAAKC,MAAMC,EAAGC,aAAaP,EAAiB,UAEhE,OADQP,EAAAe,IAAIZ,EAAIO,GACTA,EAAYN,EACrB,CAqFAY,EACGC,QAAQ,eACRC,OAAO,4BAA6B,uDACpCC,QAAOC,UACN,OAASC,GAAQL,EAAQM,KACnBC,EAAMC,QAAQD,OACdE,KAAEA,EAAO,CAAA,GAAeC,KAAgB,CAAA,GACxCvB,GAAEA,GAAOwB,IACTC,EAAgBpB,EAAKqB,SAAS1B,IAE9B2B,MACJA,EAAAC,SACAA,EAAAC,IACAA,EAAM,CAAC,EACPC,OAAQC,EAAUnC,EAAAoC,eAClBA,GACEV,EACEW,EAAc,CAClBL,SAAU,iBACVM,OAAQ,CAAC,UACNL,GAEL,IAAIC,EAASC,EACb,GAAIC,EAAgB,CAClB,MAAMG,EAAqB9B,EA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EAAgBC,EAAWD,EAAeE,EACxCF,EAAe,CAAC,YAFD,GAAGiC,GAASC,mBAAmB9B,cAEP,CAAET,uBAE3C,IAAIT,EAASD,EAAYC,OACH,mBAAXA,IAAuBA,EAASA,EAAO,CAChDhB,KAAMiC,EAAS4B,IAAK3B,KAEflB,EAAAkD,KAAK,SAASjC,KACf,MAAAkC,EAAO,GAAGJ,GAASC,2BACvBjD,EAAYL,mBAEZM,EAAOoD,KAAK,OAMd,SAASC,EAASC,GAChB,IAAKpC,EAAc,MAAA,IAAIsB,MAAM,yBAEtB,OAAArE,EAAKC,QAAQ8C,EAAQoC,EAC9B,CARAxC,EAAgBC,EAAWD,EAAeE,EACxCF,EAAe,CAAC,QAASqC,EAAM,CAAE1C,uBAQ9BjC,EAAG+E,WAAWrC,IAAS1C,EAAGgF,UAAUtC,GACnC,MAAAuC,EAAkBJ,EAAS,gBAC7B,GAAA7E,EAAG+E,WAAWE,GACV,MAAA,IAAIjB,MAAM,+BAEfhE,EAAAkF,cAAcD,EAAiB3C,GAC1B6C,QAAAC,IAAI9C,EAAe,aAAc2C,GAEnC,MAAAI,EAAUhG,EAAsBC,EAAI,WACpCgG,EAAiBT,EAAS,aACV,mBAAXzD,IACTA,EAASA,EAAO,CACdiD,IAAK3B,EACL6C,YAAazF,KAAKC,MAAMuC,MAGtB,MAAAkD,EAAgBpE,EACnBwB,QAAQ,UAAWH,GACnBG,QAAQ,aAAcyC,GACtBrF,EAAAkF,cAAcI,EAAgBE,EAAa"}
|
package/dist/index.esm.js
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
import './utils/filterSupport.esm.js';
|
2
|
-
import './commands/publish.esm.js';
|
3
2
|
import './commands/build.esm.js';
|
3
|
+
import './commands/init.esm.js';
|
4
|
+
import './commands/publish.esm.js';
|
4
5
|
import { program } from 'commander';
|
5
6
|
|
6
7
|
const pkg = require("../package.json");
|
7
|
-
program.version(pkg.version).description(pkg.description).option("--root <root>", "root path");
|
8
|
+
program.version(pkg.version).description(pkg.description).option("--root <root>", "root path").option("-c, --config-path <configPath>", "config path");
|
8
9
|
program.parse(process.argv);
|
9
10
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/index.ts"],"sourcesContent":["import './utils/filterSupport'\nimport './commands/
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/index.ts"],"sourcesContent":["import './utils/filterSupport'\nimport './commands/build'\nimport './commands/init'\nimport './commands/publish'\n\nimport { program } from 'commander'\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst pkg = require('../package.json')\n\nprogram\n .version(pkg.version)\n .description(pkg.description)\n .option('--root <root>', 'root path')\n .option('-c, --config-path <configPath>', 'config path')\n\nprogram.parse(process.argv)\n"],"names":[],"mappings":";;;;;;AAQA,MAAM,GAAA,GAAM,QAAQ,iBAAiB,CAAA,CAAA;AAErC,OAAA,CACG,OAAQ,CAAA,GAAA,CAAI,OAAO,CAAA,CACnB,YAAY,GAAI,CAAA,WAAW,CAC3B,CAAA,MAAA,CAAO,eAAiB,EAAA,WAAW,CACnC,CAAA,MAAA,CAAO,kCAAkC,aAAa,CAAA,CAAA;AAEzD,OAAQ,CAAA,KAAA,CAAM,QAAQ,IAAI,CAAA"}
|
package/dist/index.esm.min.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import"./utils/filterSupport.esm.min.js";import"./commands/
|
1
|
+
import"./utils/filterSupport.esm.min.js";import"./commands/build.esm.min.js";import"./commands/init.esm.min.js";import"./commands/publish.esm.min.js";import{program as o}from"commander";const i=require("../package.json");o.version(i.version).description(i.description).option("--root <root>","root path").option("-c, --config-path <configPath>","config path"),o.parse(process.argv);
|
2
2
|
//# sourceMappingURL=index.esm.min.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.esm.min.js","sources":["../src/index.ts"],"sourcesContent":["import './utils/filterSupport'\nimport './commands/
|
1
|
+
{"version":3,"file":"index.esm.min.js","sources":["../src/index.ts"],"sourcesContent":["import './utils/filterSupport'\nimport './commands/build'\nimport './commands/init'\nimport './commands/publish'\n\nimport { program } from 'commander'\n\n// eslint-disable-next-line @typescript-eslint/no-var-requires\nconst pkg = require('../package.json')\n\nprogram\n .version(pkg.version)\n .description(pkg.description)\n .option('--root <root>', 'root path')\n .option('-c, --config-path <configPath>', 'config path')\n\nprogram.parse(process.argv)\n"],"names":["pkg","require","program","version","description","option","parse","process","argv"],"mappings":"0LAQA,MAAMA,EAAMC,QAAQ,mBAEpBC,EACGC,QAAQH,EAAIG,SACZC,YAAYJ,EAAII,aAChBC,OAAO,gBAAiB,aACxBA,OAAO,iCAAkC,eAE5CH,EAAQI,MAAMC,QAAQC"}
|