@p8ec/shared 2.6.1 → 3.0.0

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.
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ /**
3
+ * 2026 Copyright P8 Enterprise Components, Inc.
4
+ * All Rights Reserved.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.dirn = void 0;
41
+ const path = __importStar(require("node:path"));
42
+ /**
43
+ * Returns the directory name of the caller, optionally returns a directory name specified levels up.
44
+ */
45
+ const dirn = (levelsUp) => {
46
+ const DEFAULT_LEVELS_UP = 0;
47
+ levelsUp !== null && levelsUp !== void 0 ? levelsUp : (levelsUp = `${DEFAULT_LEVELS_UP}`);
48
+ const levels = parseInt(levelsUp) || DEFAULT_LEVELS_UP;
49
+ return process.cwd().split(path.sep).reverse()[levels];
50
+ };
51
+ exports.dirn = dirn;
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ /**
3
+ * 2026 Copyright P8 Enterprise Components, Inc.
4
+ * All Rights Reserved.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
40
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
41
+ return new (P || (P = Promise))(function (resolve, reject) {
42
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
43
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
44
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
45
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
46
+ });
47
+ };
48
+ var __importDefault = (this && this.__importDefault) || function (mod) {
49
+ return (mod && mod.__esModule) ? mod : { "default": mod };
50
+ };
51
+ Object.defineProperty(exports, "__esModule", { value: true });
52
+ exports.init = exports.initCleanup = void 0;
53
+ const fs = __importStar(require("node:fs"));
54
+ const path = __importStar(require("node:path"));
55
+ const yesno_1 = __importDefault(require("../utils/yesno"));
56
+ const detect_1 = require("../utils/detect");
57
+ const p8_shared_cli_1 = require("../p8-shared-cli");
58
+ const writeLn = console.log;
59
+ const initCleanup = (packageJson) => {
60
+ writeLn('Removing eslintConfig and prettier from package.json...');
61
+ const configBackup = {};
62
+ const configBackupFile = 'p8-package-backup.json';
63
+ const configBackupSections = ['eslintConfig', 'prettier', 'commitlint'];
64
+ configBackupSections.forEach((section) => {
65
+ if (packageJson[section]) {
66
+ writeLn(`Backing up ${section} to ${section}.${configBackupFile}...`);
67
+ configBackup[section] = packageJson[section];
68
+ delete packageJson[section];
69
+ }
70
+ });
71
+ p8_shared_cli_1.cliUtils.writeFile(`${configBackupFile}`, JSON.stringify(configBackup, null, 2));
72
+ p8_shared_cli_1.cliUtils.writeFile('package.json', JSON.stringify(packageJson, null, 2));
73
+ };
74
+ exports.initCleanup = initCleanup;
75
+ /**
76
+ * Initializes a TypeScript project with P8 shared configurations.
77
+ */
78
+ const init = (option_1, ...args_1) => __awaiter(void 0, [option_1, ...args_1], void 0, function* (option, packageManager = (0, detect_1.detectPackageManager)()) {
79
+ var _a;
80
+ const packageJsonData = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8');
81
+ const packageJson = JSON.parse(packageJsonData);
82
+ const moduleType = packageJson['type'] === 'module' ? 'mjs' : 'cjs';
83
+ writeLn(`Creating eslint.config.${moduleType}...`);
84
+ p8_shared_cli_1.cliUtils.copyAsset(`eslint.config.${moduleType}`);
85
+ writeLn(`Creating prettier.config.${moduleType}...`);
86
+ p8_shared_cli_1.cliUtils.copyAsset(`prettier.config.${moduleType}`);
87
+ (_a = packageJson.scripts) !== null && _a !== void 0 ? _a : (packageJson.scripts = {});
88
+ packageJson.scripts[`${packageManager}:reset`] =
89
+ packageManager === 'pnpm'
90
+ ? 'rm -rf ./**/node_modules && rm -rf ./**/pnpm-lock.yaml && pnpm install'
91
+ : packageManager === 'yarn'
92
+ ? 'rm -rf ./**/node_modules && rm -rf ./**/yarn.lock && yarn install'
93
+ : 'rm -rf ./**/node_modules && rm -rf ./**/package-lock.json && npm install';
94
+ packageJson.scripts[`${packageManager}:audit`] =
95
+ packageManager === 'pnpm'
96
+ ? 'pnpm audit'
97
+ : packageManager === 'yarn'
98
+ ? 'yarn npm audit'
99
+ : 'npm audit --audit-level=moderate';
100
+ const lefthook = yield (0, yesno_1.default)({
101
+ question: 'Do you want to use commitlint/lefthook? [y]n',
102
+ defaultValue: true,
103
+ yesValues: ['yes', 'y'],
104
+ noValues: ['no', 'n'],
105
+ });
106
+ if (lefthook) {
107
+ writeLn(`Creating commitlint.config.${moduleType}...`);
108
+ p8_shared_cli_1.cliUtils.copyAsset(`commitlint.config.${moduleType}`);
109
+ writeLn('Creating lefthook.yml...');
110
+ p8_shared_cli_1.cliUtils.copyAsset('lefthook.yml');
111
+ writeLn('Adding lefthook install to postinstall...');
112
+ const lefthookInstall = 'lefthook install';
113
+ packageJson.scripts.postinstall = lefthookInstall;
114
+ const installCommands = {
115
+ npm: 'npm install --save-dev @commitlint/{config-conventional,cli} commitlint lefthook',
116
+ pnpm: 'pnpm install -D @commitlint/{config-conventional,cli} commitlint lefthook',
117
+ yarn: 'yarn add -D @commitlint/config-conventional @commitlint/cli commitlint lefthook',
118
+ };
119
+ const installCommand = installCommands[packageManager];
120
+ if (yield (0, yesno_1.default)({
121
+ question: `Do you want to run "${installCommand}" now? [y]n`,
122
+ defaultValue: true,
123
+ yesValues: ['yes', 'y'],
124
+ noValues: ['no', 'n'],
125
+ })) {
126
+ writeLn(`Executing ${installCommand}...`);
127
+ p8_shared_cli_1.cliUtils.execShell(installCommand);
128
+ }
129
+ else {
130
+ writeLn('You could run the following command to install needed dependencies:');
131
+ writeLn(installCommand);
132
+ }
133
+ if (yield (0, yesno_1.default)({
134
+ question: `Do you want to run "${lefthookInstall}" now? [y]n`,
135
+ defaultValue: true,
136
+ yesValues: ['yes', 'y'],
137
+ noValues: ['no', 'n'],
138
+ })) {
139
+ writeLn(`Executing ${lefthookInstall}...`);
140
+ p8_shared_cli_1.cliUtils.execShell(lefthookInstall);
141
+ }
142
+ }
143
+ if (option === null || option === void 0 ? void 0 : option.split(',').includes('cleanup')) {
144
+ (0, exports.initCleanup)(packageJson);
145
+ }
146
+ else {
147
+ writeLn('Skipping cleanup...');
148
+ p8_shared_cli_1.cliUtils.writeFile('package.json', JSON.stringify(packageJson, null, 2));
149
+ }
150
+ });
151
+ exports.init = init;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ /**
3
+ * 2026 Copyright P8 Enterprise Components, Inc.
4
+ * All Rights Reserved.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.run = void 0;
8
+ const detect_1 = require("../utils/detect");
9
+ const run = (script, packageManager, workspaceMode) => {
10
+ var _a;
11
+ if (!workspaceMode || workspaceMode === 'auto') {
12
+ workspaceMode = (0, detect_1.detectWorkspace)() ? 'seq' : 'none';
13
+ }
14
+ if (!packageManager || packageManager === 'auto') {
15
+ packageManager = (0, detect_1.detectPackageManager)();
16
+ }
17
+ const pnpmWorkspaceSeq = '-r --workspace-concurrency=1 --if-present --reporter-hide-prefix';
18
+ const pnpmWorkspacePar = '-r --if-present --parallel';
19
+ const yarnWorkspaceSeq = 'workspaces foreach -A';
20
+ const yarnWorkspacePar = 'workspaces foreach -A -p';
21
+ const commands = {
22
+ npm: {
23
+ none: `npm run ${script}`,
24
+ seq: `npm run ${script} --workspaces --if-present`,
25
+ },
26
+ yarn: {
27
+ none: `yarn run ${script}`,
28
+ seq: `yarn ${yarnWorkspaceSeq} run ${script}`,
29
+ par: `yarn ${yarnWorkspacePar} run ${script}`,
30
+ },
31
+ pnpm: {
32
+ none: `pnpm run ${script}`,
33
+ seq: `pnpm ${pnpmWorkspaceSeq} run ${script}`,
34
+ par: `pnpm ${pnpmWorkspacePar} run ${script}`,
35
+ },
36
+ };
37
+ if (!commands[packageManager]) {
38
+ throw new Error(`Unknown package manager: ${packageManager}`);
39
+ }
40
+ if (!((_a = commands[packageManager]) === null || _a === void 0 ? void 0 : _a[workspaceMode])) {
41
+ throw new Error(`Unknown workspace mode: ${workspaceMode}`);
42
+ }
43
+ return commands[packageManager][workspaceMode];
44
+ };
45
+ exports.run = run;
@@ -50,7 +50,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
50
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
51
  };
52
52
  Object.defineProperty(exports, "__esModule", { value: true });
53
- exports.run = exports.dirn = exports.init = exports.detectWorkspace = exports.detectPackageManager = exports.initCleanup = exports.cliUtils = exports.IS_DEV = void 0;
53
+ exports.cliUtils = exports.IS_DEV = exports.detectWorkspace = exports.detectPackageManager = exports.run = exports.dirn = exports.initCleanup = exports.init = void 0;
54
54
  /**
55
55
  * P8 Shared CLI tool.
56
56
  *
@@ -60,9 +60,19 @@ exports.run = exports.dirn = exports.init = exports.detectWorkspace = exports.de
60
60
  const path = __importStar(require("node:path"));
61
61
  const fs = __importStar(require("node:fs"));
62
62
  const ferramenta_1 = require("ferramenta");
63
- const yesno_1 = __importDefault(require("./utils/yesno"));
64
63
  const prompt_1 = __importDefault(require("./utils/prompt"));
64
+ const args_1 = __importDefault(require("./utils/args"));
65
65
  const child_process = __importStar(require("node:child_process"));
66
+ const init_1 = require("./cmds/init");
67
+ Object.defineProperty(exports, "init", { enumerable: true, get: function () { return init_1.init; } });
68
+ Object.defineProperty(exports, "initCleanup", { enumerable: true, get: function () { return init_1.initCleanup; } });
69
+ const dirn_1 = require("./cmds/dirn");
70
+ Object.defineProperty(exports, "dirn", { enumerable: true, get: function () { return dirn_1.dirn; } });
71
+ const run_1 = require("./cmds/run");
72
+ Object.defineProperty(exports, "run", { enumerable: true, get: function () { return run_1.run; } });
73
+ const detect_1 = require("./utils/detect");
74
+ Object.defineProperty(exports, "detectPackageManager", { enumerable: true, get: function () { return detect_1.detectPackageManager; } });
75
+ Object.defineProperty(exports, "detectWorkspace", { enumerable: true, get: function () { return detect_1.detectWorkspace; } });
66
76
  exports.IS_DEV = process.env.NODE_ENV === 'development';
67
77
  let args = ferramenta_1.processArgs.args;
68
78
  const self = path.parse(ferramenta_1.processArgs.name).name;
@@ -81,218 +91,45 @@ if (args.length === 0 && !exports.IS_DEV && require.main === module) {
81
91
  Usage: ${self} {command} [options]
82
92
 
83
93
  Commands:
84
- init [cleanup]
94
+ init [options]
85
95
  Initializes a new P8 repo.
86
96
  Options:
87
- cleanup: Removes redundant configurations from package.json.
97
+ --cleanup: Flag to remove redundant configurations from package.json.
88
98
  dirn [levelsUp]
89
99
  Returns the directory name of the caller.
90
- Options:
100
+ Arguments:
91
101
  levelsUp: The number of levels up to return the directory name.
92
- run [script [packageManager [workspaceMode]]]
102
+ run script [options]
93
103
  Returns a command to run a script with the specified package manager.
94
- Options:
104
+ Arguments:
95
105
  script: The script to run.
96
- packageManager: The package manager to use ('npm', 'yarn', 'pnpm' or 'auto'). Defaults to npm.
97
- workspaceMode: Whether to run in workspace mode ('seq', 'par', 'auto' for pnpm and yarn, and 'seq' for npm). Defaults to none.
106
+ Options:
107
+ -p {value}, --packageManager={value}: The package manager to use, where {value} is one of 'npm', 'pnpm', 'yarn', or 'auto'.
108
+ -w {value}, --workspaceMode={value}: The workspace mode to use, where {value} is one of 'none', 'seq', 'par', or 'auto'.
98
109
  `);
99
110
  if (exports.IS_DEV) {
100
111
  writeLn(`DEVELOPMENT MODE`);
101
112
  }
102
113
  process.exit(1);
103
114
  }
104
- const initCleanup = (packageJson) => {
105
- writeLn('Removing eslintConfig and prettier from package.json...');
106
- const configBackup = {};
107
- const configBackupFile = 'p8-package-backup.json';
108
- const configBackupSections = ['eslintConfig', 'prettier', 'commitlint'];
109
- configBackupSections.forEach((section) => {
110
- if (packageJson[section]) {
111
- writeLn(`Backing up ${section} to ${section}.${configBackupFile}...`);
112
- configBackup[section] = packageJson[section];
113
- delete packageJson[section];
114
- }
115
- });
116
- exports.cliUtils.writeFile(`${configBackupFile}`, JSON.stringify(configBackup, null, 2));
117
- exports.cliUtils.writeFile('package.json', JSON.stringify(packageJson, null, 2));
118
- };
119
- exports.initCleanup = initCleanup;
120
- /**
121
- * Detects the package manager used in the project.
122
- */
123
- const detectPackageManager = (cwd = process.cwd()) => {
124
- if (fs.existsSync(path.join(cwd, 'pnpm-lock.yaml'))) {
125
- return 'pnpm';
126
- }
127
- if (fs.existsSync(path.join(cwd, 'yarn.lock'))) {
128
- return 'yarn';
129
- }
130
- return 'npm';
131
- };
132
- exports.detectPackageManager = detectPackageManager;
133
- /**
134
- * Detects if the project is a workspace.
135
- */
136
- const detectWorkspace = (cwd = process.cwd()) => {
137
- if (fs.existsSync(path.join(cwd, 'pnpm-workspace.yaml'))) {
138
- return true;
139
- }
140
- const packageJsonPath = path.join(cwd, 'package.json');
141
- if (fs.existsSync(packageJsonPath)) {
142
- try {
143
- const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
144
- if (packageJson.workspaces) {
145
- return true;
146
- }
147
- }
148
- catch (_a) {
149
- return false;
150
- }
151
- }
152
- return false;
153
- };
154
- exports.detectWorkspace = detectWorkspace;
155
- /**
156
- * Initializes a TypeScript project with P8 shared configurations.
157
- */
158
- const init = (option_1, ...args_1) => __awaiter(void 0, [option_1, ...args_1], void 0, function* (option, packageManager = (0, exports.detectPackageManager)()) {
159
- var _a;
160
- const packageJsonData = fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8');
161
- const packageJson = JSON.parse(packageJsonData);
162
- const moduleType = packageJson['type'] === 'module' ? 'mjs' : 'cjs';
163
- writeLn(`Creating eslint.config.${moduleType}...`);
164
- exports.cliUtils.copyAsset(`eslint.config.${moduleType}`);
165
- writeLn(`Creating prettier.config.${moduleType}...`);
166
- exports.cliUtils.copyAsset(`prettier.config.${moduleType}`);
167
- (_a = packageJson.scripts) !== null && _a !== void 0 ? _a : (packageJson.scripts = {});
168
- packageJson.scripts[`${packageManager}:reset`] =
169
- packageManager === 'pnpm'
170
- ? 'rm -rf ./**/node_modules && rm -rf ./**/pnpm-lock.yaml && pnpm install'
171
- : packageManager === 'yarn'
172
- ? 'rm -rf ./**/node_modules && rm -rf ./**/yarn.lock && yarn install'
173
- : 'rm -rf ./**/node_modules && rm -rf ./**/package-lock.json && npm install';
174
- packageJson.scripts[`${packageManager}:audit`] =
175
- packageManager === 'pnpm'
176
- ? 'pnpm audit'
177
- : packageManager === 'yarn'
178
- ? 'yarn npm audit'
179
- : 'npm audit --audit-level=moderate';
180
- const lefthook = yield (0, yesno_1.default)({
181
- question: 'Do you want to use commitlint/lefthook? [y]n',
182
- defaultValue: true,
183
- yesValues: ['yes', 'y'],
184
- noValues: ['no', 'n'],
185
- });
186
- if (lefthook) {
187
- writeLn(`Creating commitlint.config.${moduleType}...`);
188
- exports.cliUtils.copyAsset(`commitlint.config.${moduleType}`);
189
- writeLn('Creating lefthook.yml...');
190
- exports.cliUtils.copyAsset('lefthook.yml');
191
- writeLn('Adding lefthook install to postinstall...');
192
- const lefthookInstall = 'lefthook install';
193
- packageJson.scripts.postinstall = lefthookInstall;
194
- const installCommands = {
195
- npm: 'npm install --save-dev @commitlint/{config-conventional,cli} commitlint lefthook',
196
- pnpm: 'pnpm install -D @commitlint/{config-conventional,cli} commitlint lefthook',
197
- yarn: 'yarn add -D @commitlint/config-conventional @commitlint/cli commitlint lefthook',
198
- };
199
- const installCommand = installCommands[packageManager];
200
- if (yield (0, yesno_1.default)({
201
- question: `Do you want to run "${installCommand}" now? [y]n`,
202
- defaultValue: true,
203
- yesValues: ['yes', 'y'],
204
- noValues: ['no', 'n'],
205
- })) {
206
- writeLn(`Executing ${installCommand}...`);
207
- exports.cliUtils.execShell(installCommand);
208
- }
209
- else {
210
- writeLn('You could run the following command to install needed dependencies:');
211
- writeLn(installCommand);
212
- }
213
- if (yield (0, yesno_1.default)({
214
- question: `Do you want to run "${lefthookInstall}" now? [y]n`,
215
- defaultValue: true,
216
- yesValues: ['yes', 'y'],
217
- noValues: ['no', 'n'],
218
- })) {
219
- writeLn(`Executing ${lefthookInstall}...`);
220
- exports.cliUtils.execShell(lefthookInstall);
221
- }
222
- }
223
- if (option === null || option === void 0 ? void 0 : option.split(',').includes('cleanup')) {
224
- (0, exports.initCleanup)(packageJson);
225
- }
226
- else {
227
- writeLn('Skipping cleanup...');
228
- exports.cliUtils.writeFile('package.json', JSON.stringify(packageJson, null, 2));
229
- }
230
- });
231
- exports.init = init;
232
- /**
233
- * Returns the directory name of the caller, optionally returns a directory name specified levels up.
234
- */
235
- const dirn = (levelsUp) => {
236
- const DEFAULT_LEVELS_UP = 0;
237
- levelsUp !== null && levelsUp !== void 0 ? levelsUp : (levelsUp = `${DEFAULT_LEVELS_UP}`);
238
- const levels = parseInt(levelsUp) || DEFAULT_LEVELS_UP;
239
- return process.cwd().split(path.sep).reverse()[levels];
240
- };
241
- exports.dirn = dirn;
242
- const run = (script, packageManager, workspaceMode) => {
243
- var _a;
244
- if (!workspaceMode || workspaceMode === 'auto') {
245
- workspaceMode = (0, exports.detectWorkspace)() ? 'seq' : 'none';
246
- }
247
- if (!packageManager || packageManager === 'auto') {
248
- packageManager = (0, exports.detectPackageManager)();
249
- }
250
- const pnpmWorkspaceSeq = '-r --workspace-concurrency=1 --if-present --reporter-hide-prefix';
251
- const pnpmWorkspacePar = '-r --if-present --parallel';
252
- const yarnWorkspaceSeq = 'workspaces foreach -A';
253
- const yarnWorkspacePar = 'workspaces foreach -A -p';
254
- const commands = {
255
- npm: {
256
- none: `npm run ${script}`,
257
- seq: `npm run ${script} --workspaces --if-present`,
258
- },
259
- yarn: {
260
- none: `yarn run ${script}`,
261
- seq: `yarn ${yarnWorkspaceSeq} run ${script}`,
262
- par: `yarn ${yarnWorkspacePar} run ${script}`,
263
- },
264
- pnpm: {
265
- none: `pnpm run ${script}`,
266
- seq: `pnpm ${pnpmWorkspaceSeq} run ${script}`,
267
- par: `pnpm ${pnpmWorkspacePar} run ${script}`,
268
- },
269
- };
270
- if (!commands[packageManager]) {
271
- throw new Error(`Unknown package manager: ${packageManager}`);
272
- }
273
- if (!((_a = commands[packageManager]) === null || _a === void 0 ? void 0 : _a[workspaceMode])) {
274
- throw new Error(`Unknown workspace mode: ${workspaceMode}`);
275
- }
276
- return commands[packageManager][workspaceMode];
277
- };
278
- exports.run = run;
279
115
  const main = () => __awaiter(void 0, void 0, void 0, function* () {
280
116
  // Ask the user for arguments if IS_DEV is true
281
117
  if (exports.IS_DEV) {
282
118
  args = (yield (0, prompt_1.default)('Enter arguments:')).split(' ');
283
119
  }
284
- switch (args[0]) {
120
+ const parsed = (0, args_1.default)(args);
121
+ switch (parsed.command) {
285
122
  case 'init':
286
- yield (0, exports.init)(args[1]);
123
+ yield (0, init_1.init)(parsed.positional[0] || (parsed.options.cleanup ? 'cleanup' : ''));
287
124
  break;
288
125
  case 'dirn':
289
- writeLn((0, exports.dirn)(args[1]));
126
+ writeLn((0, dirn_1.dirn)(parsed.positional[0]));
290
127
  break;
291
128
  case 'run':
292
- writeLn((0, exports.run)(args[1], args[2], args[3]));
129
+ writeLn((0, run_1.run)(parsed.positional[0], (parsed.positional[1] || parsed.options.p || parsed.options.packageManager), (parsed.positional[2] || parsed.options.w || parsed.options.workspaceMode)));
293
130
  break;
294
131
  default:
295
- console.error(`Unknown command: ${args[0]}`);
132
+ console.error(`Unknown command: ${parsed.command}`);
296
133
  process.exit(1);
297
134
  }
298
135
  });
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ /**
3
+ * 2026 Copyright P8 Enterprise Components, Inc.
4
+ * All Rights Reserved.
5
+ */
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.parseArgs = void 0;
8
+ /**
9
+ * Robust command line argument parser.
10
+ *
11
+ * @param args - Array of command line arguments.
12
+ * @returns An object containing parsed commands, options, and positional arguments.
13
+ * @example
14
+ * ```ts
15
+ * const args = ['build', '--env=production', '-f', 'config.json', 'input.txt'];
16
+ * const parsed = parseArgs(args);
17
+ * // parsed = {
18
+ * // command: 'build',
19
+ * // options: { env: 'production', f: 'config.json' },
20
+ * // positional: ['input.txt']
21
+ * // }
22
+ * ```
23
+ */
24
+ const parseArgs = (args) => {
25
+ const result = {
26
+ command: '',
27
+ options: {},
28
+ positional: [],
29
+ };
30
+ let isCommandSet = false;
31
+ for (let i = 0; i < args.length; i++) {
32
+ const arg = args[i];
33
+ if (arg.startsWith('--')) {
34
+ const [key, value] = arg.slice(2).split('=');
35
+ if (value !== undefined) {
36
+ result.options[key] = value;
37
+ }
38
+ else {
39
+ const nextArg = args[i + 1];
40
+ if (nextArg && !nextArg.startsWith('-')) {
41
+ result.options[key] = nextArg;
42
+ i++;
43
+ }
44
+ else {
45
+ result.options[key] = true;
46
+ }
47
+ }
48
+ }
49
+ else if (arg.startsWith('-')) {
50
+ const keys = arg.slice(1).split('');
51
+ keys.forEach((key, index) => {
52
+ if (index === keys.length - 1) {
53
+ const nextArg = args[i + 1];
54
+ if (nextArg && !nextArg.startsWith('-')) {
55
+ result.options[key] = nextArg;
56
+ i++;
57
+ }
58
+ else {
59
+ result.options[key] = true;
60
+ }
61
+ }
62
+ else {
63
+ result.options[key] = true;
64
+ }
65
+ });
66
+ }
67
+ else {
68
+ if (!isCommandSet) {
69
+ result.command = arg;
70
+ isCommandSet = true;
71
+ }
72
+ else {
73
+ result.positional.push(arg);
74
+ }
75
+ }
76
+ }
77
+ return result;
78
+ };
79
+ exports.parseArgs = parseArgs;
80
+ exports.default = exports.parseArgs;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ /**
3
+ * 2026 Copyright P8 Enterprise Components, Inc.
4
+ * All Rights Reserved.
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
18
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
19
+ }) : function(o, v) {
20
+ o["default"] = v;
21
+ });
22
+ var __importStar = (this && this.__importStar) || (function () {
23
+ var ownKeys = function(o) {
24
+ ownKeys = Object.getOwnPropertyNames || function (o) {
25
+ var ar = [];
26
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
27
+ return ar;
28
+ };
29
+ return ownKeys(o);
30
+ };
31
+ return function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ })();
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.detectWorkspace = exports.detectPackageManager = void 0;
41
+ const fs = __importStar(require("node:fs"));
42
+ const path = __importStar(require("node:path"));
43
+ /**
44
+ * Detects the package manager used in the project.
45
+ */
46
+ const detectPackageManager = (cwd = process.cwd()) => {
47
+ if (fs.existsSync(path.join(cwd, 'pnpm-lock.yaml'))) {
48
+ return 'pnpm';
49
+ }
50
+ if (fs.existsSync(path.join(cwd, 'yarn.lock'))) {
51
+ return 'yarn';
52
+ }
53
+ return 'npm';
54
+ };
55
+ exports.detectPackageManager = detectPackageManager;
56
+ /**
57
+ * Detects if the project is a workspace.
58
+ */
59
+ const detectWorkspace = (cwd = process.cwd()) => {
60
+ if (fs.existsSync(path.join(cwd, 'pnpm-workspace.yaml'))) {
61
+ return true;
62
+ }
63
+ const packageJsonPath = path.join(cwd, 'package.json');
64
+ if (fs.existsSync(packageJsonPath)) {
65
+ try {
66
+ const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
67
+ if (packageJson.workspaces) {
68
+ return true;
69
+ }
70
+ }
71
+ catch (_a) {
72
+ return false;
73
+ }
74
+ }
75
+ return false;
76
+ };
77
+ exports.detectWorkspace = detectWorkspace;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@p8ec/shared",
3
- "version": "2.6.1",
3
+ "version": "3.0.0",
4
4
  "description": "P(8) Global Shared Library for Javascript",
5
5
  "repository": {
6
6
  "type": "git",
@@ -29,7 +29,7 @@
29
29
  "docs:clean": "rm -rf docs"
30
30
  },
31
31
  "bin": {
32
- "p8-cli": "dist/cjs/bin/p8-shared-cli.js"
32
+ "p8cli": "dist/cjs/bin/p8-shared-cli.js"
33
33
  },
34
34
  "main": "dist/cjs/index.js",
35
35
  "module": "dist/esm/index.js",