jiek 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. package/README.md +5 -0
  2. package/dist/cli.cjs.js +668 -0
  3. package/dist/cli.cjs.js.map +1 -0
  4. package/dist/cli.cjs.min.js +2 -0
  5. package/dist/{cli.umd.min.js.map → cli.cjs.min.js.map} +1 -1
  6. package/dist/commands/publish.esm.js +1 -1
  7. package/dist/commands/publish.esm.js.map +1 -1
  8. package/dist/commands/publish.esm.min.js +1 -1
  9. package/dist/commands/publish.esm.min.js.map +1 -1
  10. package/dist/index.cjs.js +6 -0
  11. package/dist/index.cjs.js.map +1 -0
  12. package/dist/index.cjs.min.js +2 -0
  13. package/dist/index.cjs.min.js.map +1 -0
  14. package/dist/rollup/utils/externalResolver.esm.js +5 -3
  15. package/dist/rollup/utils/externalResolver.esm.js.map +1 -1
  16. package/dist/rollup/utils/externalResolver.esm.min.js +1 -1
  17. package/dist/rollup/utils/externalResolver.esm.min.js.map +1 -1
  18. package/dist/rollup/utils/globalResolver.esm.js +3 -20
  19. package/dist/rollup/utils/globalResolver.esm.js.map +1 -1
  20. package/dist/rollup/utils/globalResolver.esm.min.js +1 -1
  21. package/dist/rollup/utils/globalResolver.esm.min.js.map +1 -1
  22. package/dist/rollup.cjs.js +227 -0
  23. package/dist/rollup.cjs.js.map +1 -0
  24. package/dist/rollup.cjs.min.js +2 -0
  25. package/dist/rollup.cjs.min.js.map +1 -0
  26. package/dist/rollup.d.ts +3 -0
  27. package/dist/rollup.esm.d.ts +3 -0
  28. package/dist/rollup.esm.js +11 -1
  29. package/dist/rollup.esm.js.map +1 -1
  30. package/dist/rollup.esm.min.js +1 -1
  31. package/dist/rollup.esm.min.js.map +1 -1
  32. package/package.json +4 -1
  33. package/dist/cli.iife.js +0 -658
  34. package/dist/cli.iife.js.map +0 -1
  35. package/dist/cli.iife.min.js +0 -2
  36. package/dist/cli.iife.min.js.map +0 -1
  37. package/dist/cli.umd.js +0 -661
  38. package/dist/cli.umd.js.map +0 -1
  39. package/dist/cli.umd.min.js +0 -2
  40. package/dist/index.iife.js +0 -11
  41. package/dist/index.iife.js.map +0 -1
  42. package/dist/index.iife.min.js +0 -2
  43. package/dist/index.iife.min.js.map +0 -1
  44. package/dist/index.umd.js +0 -12
  45. package/dist/index.umd.js.map +0 -1
  46. package/dist/index.umd.min.js +0 -2
  47. package/dist/index.umd.min.js.map +0 -1
  48. package/dist/rollup.iife.js +0 -228
  49. package/dist/rollup.iife.js.map +0 -1
  50. package/dist/rollup.iife.min.js +0 -2
  51. package/dist/rollup.iife.min.js.map +0 -1
  52. package/dist/rollup.umd.js +0 -229
  53. package/dist/rollup.umd.js.map +0 -1
  54. package/dist/rollup.umd.min.js +0 -2
  55. package/dist/rollup.umd.min.js.map +0 -1
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Jiek
2
+
3
+ A powerful and clever monorepo cli tool.
4
+
5
+ ## Features
@@ -0,0 +1,668 @@
1
+ 'use strict';
2
+
3
+ var fs = require('node:fs');
4
+ var path = require('node:path');
5
+ var filterWorkspacePackages = require('@pnpm/filter-workspace-packages');
6
+ var commander = require('commander');
7
+ var jsYaml = require('js-yaml');
8
+ var getWorkspaceDir = require('@jiek/utils/getWorkspaceDir');
9
+ var childProcess = require('node:child_process');
10
+ var pkger = require('@jiek/pkger');
11
+ var detectIndent = require('detect-indent');
12
+ var inquirer = require('inquirer');
13
+ var jsoncParser = require('jsonc-parser');
14
+ var micromatch = require('micromatch');
15
+ var bumper = require('@jiek/utils/bumper');
16
+
17
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
18
+
19
+ function _interopNamespace(e) {
20
+ if (e && e.__esModule) return e;
21
+ var n = Object.create(null);
22
+ if (e) {
23
+ Object.keys(e).forEach(function (k) {
24
+ if (k !== 'default') {
25
+ var d = Object.getOwnPropertyDescriptor(e, k);
26
+ Object.defineProperty(n, k, d.get ? d : {
27
+ enumerable: true,
28
+ get: function () { return e[k]; }
29
+ });
30
+ }
31
+ });
32
+ }
33
+ n.default = e;
34
+ return Object.freeze(n);
35
+ }
36
+
37
+ var fs__default = /*#__PURE__*/_interopDefault(fs);
38
+ var path__default = /*#__PURE__*/_interopDefault(path);
39
+ var childProcess__namespace = /*#__PURE__*/_interopNamespace(childProcess);
40
+ var detectIndent__default = /*#__PURE__*/_interopDefault(detectIndent);
41
+ var inquirer__default = /*#__PURE__*/_interopDefault(inquirer);
42
+
43
+ let root;
44
+ function getRoot() {
45
+ if (root)
46
+ return root;
47
+ const rootOption = commander.program.getOptionValue("root");
48
+ root = rootOption ? path__default.default.isAbsolute(rootOption) ? rootOption : path__default.default.resolve(process.cwd(), rootOption) : process.cwd();
49
+ return root;
50
+ }
51
+
52
+ let wd;
53
+ let notWorkspace = false;
54
+ function getWD() {
55
+ if (wd)
56
+ return { wd, notWorkspace };
57
+ const root = getRoot();
58
+ try {
59
+ wd = getWorkspaceDir.getWorkspaceDir(root, type);
60
+ } catch (e) {
61
+ if ("message" in e && e.message === "workspace root not found") {
62
+ wd = root;
63
+ notWorkspace = true;
64
+ } else {
65
+ throw e;
66
+ }
67
+ }
68
+ return { wd, notWorkspace };
69
+ }
70
+
71
+ let type = "";
72
+ try {
73
+ require.resolve("@pnpm/filter-workspace-packages");
74
+ type = "pnpm";
75
+ } catch {
76
+ }
77
+ if (type !== "") {
78
+ commander.program.option("-f, --filter <filter>", "filter packages");
79
+ }
80
+ async function getSelectedProjectsGraph() {
81
+ let filter = commander.program.getOptionValue("filter");
82
+ const root = getRoot();
83
+ const { wd, notWorkspace } = getWD();
84
+ if (!notWorkspace && type === "pnpm") {
85
+ const pnpmWorkspaceFilePath = path__default.default.resolve(wd, "pnpm-workspace.yaml");
86
+ const pnpmWorkspaceFileContent = fs__default.default.readFileSync(pnpmWorkspaceFilePath, "utf-8");
87
+ const pnpmWorkspace = jsYaml.load(pnpmWorkspaceFileContent);
88
+ if (root === wd && !filter) {
89
+ throw new Error("root path is workspace root, please provide a filter");
90
+ }
91
+ if (root !== wd && !filter) {
92
+ const packageJSONIsExist = fs__default.default.existsSync(path__default.default.resolve(root, "package.json"));
93
+ if (!packageJSONIsExist) {
94
+ throw new Error("root path is not workspace root, please provide a filter");
95
+ }
96
+ const packageJSON = JSON.parse(fs__default.default.readFileSync(path__default.default.resolve(root, "package.json"), "utf-8"));
97
+ if (!packageJSON.name) {
98
+ throw new Error("root path is not workspace root, please provide a filter");
99
+ }
100
+ filter = packageJSON.name;
101
+ }
102
+ const { selectedProjectsGraph } = await filterWorkspacePackages.filterPackagesFromDir(wd, [{
103
+ filter: filter ?? "",
104
+ followProdDepsOnly: true
105
+ }], {
106
+ prefix: root,
107
+ workspaceDir: wd,
108
+ patterns: pnpmWorkspace.packages
109
+ });
110
+ return {
111
+ wd,
112
+ root,
113
+ value: Object.entries(selectedProjectsGraph).reduce((acc, [key, value]) => {
114
+ acc[key] = value.package.manifest;
115
+ return acc;
116
+ }, {})
117
+ };
118
+ }
119
+ return {
120
+ wd,
121
+ root,
122
+ value: {
123
+ [wd]: JSON.parse(fs__default.default.readFileSync(path__default.default.resolve(wd, "package.json"), "utf-8"))
124
+ }
125
+ };
126
+ }
127
+
128
+ let resolve;
129
+ function actionDone() {
130
+ resolve();
131
+ }
132
+ function actionRestore() {
133
+ new Promise((r) => resolve = r);
134
+ }
135
+
136
+ function commondir(files, cwd = process.cwd()) {
137
+ const resolvedFiles = files.map((file) => {
138
+ if (path__default.default.isAbsolute(file))
139
+ return file;
140
+ return path__default.default.resolve(cwd, file);
141
+ });
142
+ const sep = "/";
143
+ const [first = ""] = resolvedFiles;
144
+ const parts = first.split(sep);
145
+ let common = "";
146
+ for (let i = 0; i < parts.length; i++) {
147
+ const segment = parts[i];
148
+ if (resolvedFiles.every((file) => file.startsWith(common + segment))) {
149
+ common += segment + sep;
150
+ } else {
151
+ break;
152
+ }
153
+ }
154
+ return common;
155
+ }
156
+
157
+ function mergePackageJson(manifest, cwd) {
158
+ const {
159
+ jiek: { cwd: _, ...jiek } = {}
160
+ } = manifest;
161
+ let { exports } = manifest;
162
+ let includeIndex = false;
163
+ if (typeof exports === "string") {
164
+ includeIndex = true;
165
+ exports = { ".": exports };
166
+ }
167
+ if (exports === void 0) {
168
+ exports = { ".": "./src/index.ts" };
169
+ }
170
+ if (typeof exports === "object") {
171
+ if (Array.isArray(exports) && exports.length > 0) {
172
+ includeIndex = true;
173
+ } else {
174
+ includeIndex = !!exports["."];
175
+ }
176
+ }
177
+ const inputs = Array.isArray(exports) ? exports : Object.entries(exports).reduce((acc, [key, value]) => {
178
+ if (typeof value === "string")
179
+ return key === "." ? [value, ...acc] : acc.concat(value);
180
+ if (Array.isArray(value))
181
+ return acc.concat(value);
182
+ throw new TypeError(`Unexpected value type for key "${key}" in exports, expected string, got ${typeof value}`);
183
+ }, []);
184
+ if (inputs.length === 0)
185
+ throw new Error("No inputs found");
186
+ const absoluteInputs = inputs.map(
187
+ (input) => path__default.default.isAbsolute(input) ? input : path__default.default.resolve(cwd, input)
188
+ );
189
+ let cDir = path__default.default.dirname(absoluteInputs[0]);
190
+ if (absoluteInputs.length > 1) {
191
+ cDir = commondir(absoluteInputs, cwd);
192
+ }
193
+ const resolvedInputs = absoluteInputs.map((input) => {
194
+ return path__default.default.relative(cDir, input);
195
+ });
196
+ return {
197
+ ...manifest,
198
+ ...pkger.pkger({
199
+ cwd,
200
+ noIndex: !includeIndex,
201
+ source: path__default.default.relative(cwd, cDir),
202
+ inputs: resolvedInputs,
203
+ ...jiek
204
+ })
205
+ };
206
+ }
207
+
208
+ function packageIsExist(name) {
209
+ try {
210
+ require.resolve(name);
211
+ return true;
212
+ } catch (e) {
213
+ return false;
214
+ }
215
+ }
216
+ let tsRegisterName;
217
+ const registers = [
218
+ process.env.JIEK_TS_REGISTER,
219
+ "esbuild-register",
220
+ "@swc-node/register",
221
+ "ts-node/register"
222
+ ].filter(Boolean);
223
+ for (const register of registers) {
224
+ if (packageIsExist(register)) {
225
+ tsRegisterName = register;
226
+ break;
227
+ }
228
+ }
229
+
230
+ const FILE_TEMPLATE = (manifest) => `
231
+ const pkg = ${JSON.stringify(manifest, null, 2)}
232
+ const { jiek = {} } = pkg
233
+ const templateArg = jiek.templateArgFilePath
234
+ ? require.resolve(jiek.templateArgFilePath)
235
+ : {
236
+ styled: jiek.styled
237
+ }
238
+ module.exports = require('jiek/rollup').template(templateArg, pkg)
239
+ `.trimStart();
240
+ commander.program.command("build").action(async () => {
241
+ actionRestore();
242
+ const {
243
+ wd,
244
+ value = {}
245
+ } = await getSelectedProjectsGraph() ?? {};
246
+ if (Object.keys(value).length === 0) {
247
+ throw new Error("no package found");
248
+ }
249
+ const jiekTempDir = (...paths) => path__default.default.resolve(wd, "node_modules/.jiek", ...paths);
250
+ if (!fs__default.default.existsSync(jiekTempDir()))
251
+ fs__default.default.mkdirSync(jiekTempDir());
252
+ const rollupBinaryPath = require.resolve("rollup").replace(/dist\/rollup.js$/, "dist/bin/rollup");
253
+ let i = 0;
254
+ for (const [dir, manifest] of Object.entries(value)) {
255
+ const newManifest = mergePackageJson(manifest, dir);
256
+ const escapeManifestName = manifest.name?.replace(/^@/g, "").replace(/\//g, "+");
257
+ const configFile = jiekTempDir(
258
+ `${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
259
+ );
260
+ fs__default.default.writeFileSync(configFile, FILE_TEMPLATE(newManifest));
261
+ let prefix = "";
262
+ if (tsRegisterName) {
263
+ prefix = `node -r ${tsRegisterName} `;
264
+ }
265
+ childProcess__namespace.execSync(`${prefix}${rollupBinaryPath} -c ${configFile}`, {
266
+ cwd: dir,
267
+ stdio: "inherit"
268
+ });
269
+ }
270
+ actionDone();
271
+ });
272
+
273
+ let configName = "jiek.config";
274
+ function getConfigPath(root) {
275
+ const isSupportTsLoader = !!tsRegisterName;
276
+ function configWithExtIsExist(ext) {
277
+ const filenames = [
278
+ path__default.default.resolve(root, `${configName}.${ext}`),
279
+ path__default.default.resolve(root, `.${configName}.${ext}`)
280
+ ];
281
+ for (const filename of filenames) {
282
+ if (fs__default.default.existsSync(filename) && fs__default.default.lstatSync(filename).isFile()) {
283
+ return filename;
284
+ }
285
+ }
286
+ return;
287
+ }
288
+ configName = configWithExtIsExist("js") ?? configName;
289
+ configName = configWithExtIsExist("json") ?? configName;
290
+ configName = configWithExtIsExist("yaml") ?? configName;
291
+ if (isSupportTsLoader) {
292
+ configName = configWithExtIsExist("ts") ?? configName;
293
+ }
294
+ return path__default.default.resolve(root, configName);
295
+ }
296
+ function loadConfig() {
297
+ const { wd: root, notWorkspace } = getWD();
298
+ if (notWorkspace)
299
+ throw new Error("not in workspace");
300
+ let configPath = commander.program.getOptionValue("configPath");
301
+ if (!configPath) {
302
+ configPath = getConfigPath(root);
303
+ } else {
304
+ if (!fs__default.default.existsSync(configPath))
305
+ throw new Error(`config file not found: ${configPath}`);
306
+ if (!path__default.default.isAbsolute(configPath))
307
+ configPath = path__default.default.resolve(root, configPath);
308
+ }
309
+ const ext = path__default.default.extname(configPath);
310
+ let module;
311
+ switch (ext) {
312
+ case ".js":
313
+ module = require(configPath);
314
+ break;
315
+ case ".json":
316
+ return require(configPath);
317
+ case ".yaml":
318
+ return jsYaml.load(fs__default.default.readFileSync(configPath, "utf-8"));
319
+ case ".ts":
320
+ if (tsRegisterName) {
321
+ require(tsRegisterName);
322
+ module = require(configPath);
323
+ break;
324
+ }
325
+ throw new Error(
326
+ "ts config file is not supported without ts register, please install esbuild-register or set JIEK_TS_REGISTER env for custom ts register"
327
+ );
328
+ case ".config":
329
+ module = {};
330
+ break;
331
+ default:
332
+ throw new Error(`unsupported config file type: ${ext}`);
333
+ }
334
+ if (!module)
335
+ throw new Error("config file is empty");
336
+ return module.default ?? module;
337
+ }
338
+
339
+ const PACKAGE_JSON_TEMPLATE = `{
340
+ "name": "",
341
+ "version": "0.0.1",
342
+ "description": "",
343
+ "license": "",
344
+ "author": "",
345
+ "files": ["dist"],
346
+ "exports": {
347
+ ".": "./src/index.ts"
348
+ },
349
+ "scripts": {
350
+ },
351
+ "homepage": "",
352
+ "repository": "",
353
+ "bugs": ""
354
+ }`.trimStart();
355
+ const README_TEMPLATE = `# $name
356
+
357
+ ## Installation
358
+
359
+ \`\`\`bash
360
+ npm install $name
361
+ # or
362
+ pnpm install $name
363
+ # or
364
+ yarn add $name
365
+ \`\`\`
366
+
367
+ ## Usage
368
+
369
+
370
+ ## License
371
+
372
+ $license
373
+ `.trimStart();
374
+ function getTemplateStr(wd, template) {
375
+ let templateString = template ?? PACKAGE_JSON_TEMPLATE;
376
+ let isTemplateFile = false;
377
+ try {
378
+ if (template)
379
+ JSON.parse(template);
380
+ } catch (e) {
381
+ isTemplateFile = true;
382
+ }
383
+ if (isTemplateFile) {
384
+ const templatePath = path__default.default.resolve(wd, template);
385
+ templateString = fs__default.default.readFileSync(templatePath, "utf-8");
386
+ }
387
+ return templateString;
388
+ }
389
+ const wdCache = /* @__PURE__ */ new Map();
390
+ function getWDPackageJSONFiled(wd, field) {
391
+ if (wdCache.has(wd)) {
392
+ return wdCache.get(wd)[field];
393
+ }
394
+ const packageJSONPath = path__default.default.resolve(wd, "package.json");
395
+ const packageJSON = JSON.parse(fs__default.default.readFileSync(packageJSONPath, "utf-8"));
396
+ wdCache.set(wd, packageJSON);
397
+ return packageJSON[field];
398
+ }
399
+ async function getName(named, name, {
400
+ wd,
401
+ cwd,
402
+ workspaceName
403
+ }) {
404
+ const relativePath = cwd.replace(`${wd}/`, "");
405
+ let basename = path__default.default.basename(cwd);
406
+ if (typeof named === "function") {
407
+ return named(name, {
408
+ full: wd,
409
+ relative: cwd
410
+ });
411
+ }
412
+ let isParentMatched = false;
413
+ let matchedKey;
414
+ let matchedRule;
415
+ if (typeof named === "object") {
416
+ const isWD = cwd === wd;
417
+ if (isWD) {
418
+ const { rule } = await inquirer__default.default.prompt({
419
+ type: "list",
420
+ name: "rule",
421
+ message: "choose a rule",
422
+ default: "default",
423
+ choices: ["default"].concat(Object.keys(named))
424
+ });
425
+ if (rule !== "default") {
426
+ matchedKey = rule;
427
+ matchedRule = named[rule];
428
+ }
429
+ } else
430
+ for (const [key, value] of Object.entries(named)) {
431
+ if (micromatch.isMatch(relativePath, key)) {
432
+ matchedKey = key;
433
+ matchedRule = value;
434
+ break;
435
+ }
436
+ if (micromatch.isMatch(`${relativePath}/jiek_ignore_dont_use_same_file_name`, key)) {
437
+ isParentMatched = true;
438
+ matchedKey = key;
439
+ matchedRule = value;
440
+ break;
441
+ }
442
+ }
443
+ }
444
+ if (!matchedRule) {
445
+ matchedKey = "packages/*";
446
+ matchedRule = `@${workspaceName}/$basename`;
447
+ }
448
+ if (!matchedRule)
449
+ throw new Error("no matched rule");
450
+ if (!name && isParentMatched) {
451
+ basename = await inquirer__default.default.prompt({
452
+ type: "input",
453
+ name: "name",
454
+ message: `the matched rule is \`${String(matchedRule)}\`, please input the basename
455
+ `
456
+ }).then(({ name: name2 }) => name2);
457
+ }
458
+ if (typeof matchedRule === "function") {
459
+ return matchedRule(name, {
460
+ full: wd,
461
+ relative: cwd,
462
+ basename
463
+ });
464
+ }
465
+ if (typeof matchedRule === "string") {
466
+ const dirName = name ?? basename;
467
+ return [
468
+ matchedRule.replace(/\$basename/g, dirName),
469
+ matchedKey?.replace(/\/\*$/g, `/${dirName}`)
470
+ ];
471
+ }
472
+ throw new Error("no matched rule");
473
+ }
474
+ commander.program.command("init [name]").option("-t, --template <template>", "the package.json template file path or file content").action(async () => {
475
+ const [, name] = commander.program.args;
476
+ const cwd = process.cwd();
477
+ const { init = {} } = loadConfig() ?? {};
478
+ const { wd } = getWD();
479
+ const workspaceName = path__default.default.basename(wd);
480
+ const {
481
+ named,
482
+ template,
483
+ bug = {},
484
+ readme: _readme = README_TEMPLATE,
485
+ readmeTemplate
486
+ } = init;
487
+ const resolvedBug = {
488
+ template: "bug_report.yml",
489
+ labels: ["bug"],
490
+ ...bug
491
+ };
492
+ let readme = _readme;
493
+ if (readmeTemplate) {
494
+ const readmeTemplatePath = path__default.default.resolve(wd, readmeTemplate);
495
+ readme = fs__default.default.readFileSync(readmeTemplatePath, "utf-8");
496
+ }
497
+ const templateString = getTemplateStr(wd, template);
498
+ const { indent = " " } = detectIndent__default.default(templateString);
499
+ const formattingOptions = {
500
+ tabSize: indent.length,
501
+ insertSpaces: true
502
+ };
503
+ const passFields = [
504
+ "license",
505
+ "author"
506
+ ];
507
+ let newJSONString = templateString;
508
+ for (const field of passFields) {
509
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
510
+ newJSONString,
511
+ [field],
512
+ getWDPackageJSONFiled(wd, field),
513
+ { formattingOptions }
514
+ ));
515
+ }
516
+ let [pkgName, pkgDir] = await getName(named, name, {
517
+ wd,
518
+ cwd,
519
+ workspaceName
520
+ });
521
+ if (!pkgDir) {
522
+ const { dir } = await inquirer__default.default.prompt({
523
+ type: "input",
524
+ name: "dir",
525
+ message: "package directory",
526
+ default: name
527
+ });
528
+ pkgDir = dir;
529
+ }
530
+ if (!pkgName) {
531
+ const { name: inputName } = await inquirer__default.default.prompt({
532
+ type: "input",
533
+ name: "name",
534
+ message: "package name",
535
+ default: name
536
+ });
537
+ pkgName = inputName;
538
+ }
539
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(newJSONString, ["name"], pkgName, { formattingOptions }));
540
+ let pkgRepo = getWDPackageJSONFiled(wd, "repository");
541
+ if (typeof pkgRepo === "string") {
542
+ pkgRepo = {
543
+ type: "git",
544
+ url: pkgRepo,
545
+ directory: pkgDir
546
+ };
547
+ }
548
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
549
+ newJSONString,
550
+ ["repository"],
551
+ pkgRepo,
552
+ { formattingOptions }
553
+ ));
554
+ const homepage = `${pkgRepo?.url}/blob/master/${pkgDir}/README.md`;
555
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
556
+ newJSONString,
557
+ ["homepage"],
558
+ homepage,
559
+ { formattingOptions }
560
+ ));
561
+ let labels = resolvedBug.labels;
562
+ if (typeof labels === "function")
563
+ labels = labels({
564
+ name: pkgName,
565
+ dir: pkgDir
566
+ });
567
+ labels.push(`scope:${pkgName}`);
568
+ const bugs = `${pkgRepo?.url}/issues/new?template=${resolvedBug.template}&labels=${labels.join(",")}`;
569
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
570
+ newJSONString,
571
+ ["bugs"],
572
+ bugs,
573
+ { formattingOptions }
574
+ ));
575
+ function pkgDirTo(to) {
576
+ if (!pkgDir)
577
+ throw new Error("pkgDir is not defined");
578
+ return path__default.default.resolve(pkgDir, to);
579
+ }
580
+ if (!fs__default.default.existsSync(pkgDir))
581
+ fs__default.default.mkdirSync(pkgDir);
582
+ const pkgJSONFilePath = pkgDirTo("package.json");
583
+ if (fs__default.default.existsSync(pkgJSONFilePath)) {
584
+ throw new Error("package.json already exists");
585
+ }
586
+ fs__default.default.writeFileSync(pkgJSONFilePath, newJSONString);
587
+ console.log(newJSONString, "written to", pkgJSONFilePath);
588
+ const license = getWDPackageJSONFiled(wd, "license");
589
+ const readmeFilePath = pkgDirTo("README.md");
590
+ if (typeof readme === "function") {
591
+ readme = readme({
592
+ dir: pkgDir,
593
+ packageJson: JSON.parse(newJSONString)
594
+ });
595
+ }
596
+ const readmeContent = readme.replace(/\$name/g, pkgName).replace(/\$license/g, license);
597
+ fs__default.default.writeFileSync(readmeFilePath, readmeContent);
598
+ });
599
+
600
+ commander.program.command("publish").aliases(["pub", "p"]).option("-b, --bumper <bumper>", "bump version", "patch").option("-p, --preview", "preview publish").action(async ({ preview, bumper: bumper$1, ...options }) => {
601
+ actionRestore();
602
+ const { value = {} } = await getSelectedProjectsGraph() ?? {};
603
+ const selectedProjectsGraphEntries = Object.entries(value);
604
+ if (selectedProjectsGraphEntries.length === 0) {
605
+ throw new Error("no packages selected");
606
+ }
607
+ const mainfests = selectedProjectsGraphEntries.map(([dir, manifest]) => [
608
+ dir,
609
+ mergePackageJson(manifest, dir)
610
+ ]);
611
+ const passArgs = Object.entries(options).reduce((acc, [key, value2]) => {
612
+ if (value2) {
613
+ acc.push(`--${key}`, value2);
614
+ }
615
+ return acc;
616
+ }, []);
617
+ for (const [dir, manifest] of mainfests) {
618
+ const oldJSONString = fs__default.default.readFileSync(path__default.default.join(dir, "package.json"), "utf-8");
619
+ const oldJSON = JSON.parse(oldJSONString) ?? "0.0.0";
620
+ const newVersion = bumper.bump(oldJSON.version, bumper$1);
621
+ const { indent = " " } = detectIndent__default.default(oldJSONString);
622
+ const formattingOptions = {
623
+ tabSize: indent.length,
624
+ insertSpaces: true
625
+ };
626
+ let newJSONString = oldJSONString;
627
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
628
+ newJSONString,
629
+ ["version"],
630
+ newVersion,
631
+ { formattingOptions }
632
+ ));
633
+ for (const [key, value2] of Object.entries(manifest)) {
634
+ if (JSON.stringify(value2) === JSON.stringify(oldJSON[key]))
635
+ continue;
636
+ newJSONString = jsoncParser.applyEdits(newJSONString, jsoncParser.modify(
637
+ newJSONString,
638
+ ["publishConfig", key],
639
+ value2,
640
+ { formattingOptions }
641
+ ));
642
+ }
643
+ try {
644
+ fs__default.default.renameSync(path__default.default.join(dir, "package.json"), path__default.default.join(dir, "package.json.bak"));
645
+ fs__default.default.writeFileSync(path__default.default.join(dir, "package.json"), newJSONString);
646
+ console.log(newJSONString);
647
+ if (preview) {
648
+ console.warn("preview mode");
649
+ continue;
650
+ }
651
+ childProcess__namespace.execSync(["pnpm", "publish", "--access", "public", "--no-git-checks", ...passArgs].join(" "), {
652
+ cwd: dir,
653
+ stdio: "inherit"
654
+ });
655
+ const modifyVersionPackageJSON = jsoncParser.applyEdits(oldJSONString, jsoncParser.modify(oldJSONString, ["version"], newVersion, {}));
656
+ fs__default.default.writeFileSync(path__default.default.join(dir, "package.json.bak"), modifyVersionPackageJSON);
657
+ } finally {
658
+ fs__default.default.unlinkSync(path__default.default.join(dir, "package.json"));
659
+ fs__default.default.renameSync(path__default.default.join(dir, "package.json.bak"), path__default.default.join(dir, "package.json"));
660
+ }
661
+ }
662
+ actionDone();
663
+ });
664
+
665
+ const pkg = require("../package.json");
666
+ commander.program.version(pkg.version).description(pkg.description).option("--root <root>", "root path").option("-c, --config-path <configPath>", "config path");
667
+ commander.program.parse(process.argv);
668
+ //# sourceMappingURL=cli.cjs.js.map