unipm 1.1.7 → 1.1.8

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "monorepo": true,
3
3
  "name": "unipm",
4
- "version": "1.1.7",
4
+ "version": "1.1.8",
5
5
  "type": "module",
6
6
  "main": "./lib/_export_all_in_one_index.cjs",
7
7
  "typings": "./docs/package-public.d.ts",
@@ -24,23 +24,23 @@
24
24
  },
25
25
  "preferGlobal": true,
26
26
  "dependencies": {
27
- "@idlebox/node-json-edit": "^2.0.19",
28
- "@idlebox/common": "^1.3.4",
29
- "@idlebox/node": "^1.2.3",
30
- "tslib": "^2.3.1",
27
+ "@idlebox/node-json-edit": "workspace:^2.0.20",
28
+ "@idlebox/common": "workspace:^1.3.8",
29
+ "@idlebox/node": "workspace:^1.2.5",
30
+ "tslib": "^2.4.0",
31
31
  "execa": "^6.1.0",
32
- "json5": "^2.2.0",
33
- "pacote": "^13.0.5",
32
+ "json5": "^2.2.1",
33
+ "pacote": "^13.6.1",
34
34
  "source-map-support": "^0.5.21"
35
35
  },
36
36
  "devDependencies": {
37
- "@build-script/builder": "^3.2.4",
38
- "@build-script/export-all-in-one": "^3.1.2",
39
- "@build-script/typescript-transformer-dual-package": "^1.0.25",
40
- "@build-script/single-dog-asset": "^1.0.16",
41
- "@types/node": "^17.0.21",
42
- "npm-check-updates": "^12.5.3",
43
- "typescript": "^4.6.2",
37
+ "@build-script/builder": "workspace:^3.2.5",
38
+ "@build-script/export-all-in-one": "workspace:^3.1.3",
39
+ "@build-script/typescript-transformer-dual-package": "workspace:^1.0.26",
40
+ "@build-script/single-dog-asset": "workspace:^1.0.18",
41
+ "@types/node": "^18.0.0",
42
+ "npm-check-updates": "^14.0.2",
43
+ "typescript": "^4.7.4",
44
44
  "gulp": "^4.0.2",
45
45
  "ttypescript": "^1.5.13"
46
46
  },
@@ -1,124 +0,0 @@
1
- import { createInterface } from 'readline';
2
- import { KNOWN_PACKAGE_MANAGERS } from './getPackageManagerByName';
3
- import { PackageManager } from './packageManager';
4
-
5
- export interface IGetPackageManagerOptions {
6
- cwd: string;
7
- default: 'npm' | 'yarn' | 'rush' | 'cnpm' | 'auto';
8
- ask: boolean;
9
- }
10
-
11
- export async function getPackageManager(_options?: Partial<IGetPackageManagerOptions>): Promise<PackageManager> {
12
- const options: IGetPackageManagerOptions = Object.assign(
13
- {},
14
- {
15
- cwd: process.cwd(),
16
- default: 'auto',
17
- ask: true,
18
- },
19
- _options || {}
20
- );
21
-
22
- const all: PackageManager[] = KNOWN_PACKAGE_MANAGERS.map((Manager) => {
23
- return new Manager(options.cwd);
24
- });
25
-
26
- const detected = await new Promise<PackageManager | undefined>((resolve) => {
27
- let ps = all.map((pm) => {
28
- return pm.detect().then((found) => {
29
- if (found) {
30
- resolve(pm);
31
- }
32
- });
33
- });
34
- Promise.all(ps).finally(() => resolve(undefined));
35
- });
36
-
37
- await Promise.all(
38
- all.map((pm) => {
39
- return pm.detect().then((found) => {
40
- return found ? pm : undefined;
41
- });
42
- })
43
- );
44
- if (detected) {
45
- return detected;
46
- }
47
-
48
- const installed = (
49
- await Promise.all(
50
- all.map((pm) => {
51
- return pm.exists().then((found) => {
52
- return found ? pm : undefined;
53
- });
54
- })
55
- )
56
- ).filter((e) => !!e) as PackageManager[];
57
-
58
- if (process.env.PREFER_NODE_PM) {
59
- const pmi = installed.findIndex((item) => item.friendlyName === process.env.PREFER_NODE_PM);
60
- if (pmi !== 0) {
61
- const [pm] = installed.splice(pmi, 1);
62
- installed.unshift(pm);
63
- }
64
- }
65
-
66
- if (options.ask && process.stdin.isTTY) {
67
- const selection = await askUserSelect(installed);
68
- if (selection) {
69
- return selection;
70
- }
71
- }
72
-
73
- if (options.default === 'auto') {
74
- if (installed.length) {
75
- return installed[0];
76
- } else {
77
- return all[0];
78
- }
79
- }
80
-
81
- for (const item of installed) {
82
- if (item.friendlyName === options.default) {
83
- return item;
84
- }
85
- }
86
-
87
- return all[0];
88
- }
89
-
90
- async function askUserSelect(installed: PackageManager[]): Promise<PackageManager | undefined> {
91
- if (installed.length === 0) {
92
- return undefined;
93
- }
94
- console.error(`Can't detect package manager type, please select one from below:`);
95
- for (const [index, item] of installed.entries()) {
96
- console.error(' \x1B[38;5;14m[%s]\x1B[0m: %s', index, item.friendlyName);
97
- }
98
- console.error('> ');
99
-
100
- const rl = createInterface({
101
- input: process.stdin,
102
- output: process.stdout,
103
- terminal: true,
104
- historySize: 0,
105
- prompt: '> ',
106
- });
107
-
108
- let selection: number = -1;
109
- await new Promise((resolve) => {
110
- rl.on('line', (line) => {
111
- selection = parseInt(line);
112
- if (installed[selection]) {
113
- resolve(selection);
114
- } else {
115
- console.error(`Unknown selection: "${line}"`);
116
- console.error('> ');
117
- }
118
- });
119
- });
120
-
121
- rl.close();
122
-
123
- return installed[selection];
124
- }
@@ -1,16 +0,0 @@
1
- import { Npm } from '../package-managers/npm';
2
- import { Pnpm } from '../package-managers/pnpm';
3
- import { Rush } from '../package-managers/rush';
4
- import { Yarn } from '../package-managers/yarn';
5
- import { PackageManagerConstructor } from './packageManager';
6
-
7
- export const KNOWN_PACKAGE_MANAGERS: PackageManagerConstructor[] = [Npm, Yarn, Pnpm, Rush];
8
-
9
- export function getPackageManagerByName(name: string): PackageManagerConstructor | undefined {
10
- for (const ctr of KNOWN_PACKAGE_MANAGERS) {
11
- if (name.toLowerCase() === ctr.name.toLowerCase()) {
12
- return ctr;
13
- }
14
- }
15
- return undefined;
16
- }
@@ -1,42 +0,0 @@
1
- import { loadJsonFile, writeJsonFileBack } from '@idlebox/node-json-edit';
2
- import { sortByString } from '@idlebox/common';
3
-
4
- export async function resortPackage(file: string) {
5
- const original: any = await loadJsonFile(file);
6
- for (const k of ['devDependencies', 'dependencies']) {
7
- if (original[k]) {
8
- original[k] = sort(original[k]);
9
- }
10
- }
11
- await writeJsonFileBack(original);
12
- }
13
-
14
- function sort(obj: any): any {
15
- const ret: any = {};
16
- const sortedKey = Object.keys(obj).sort(sortByString);
17
- for (const k of sortedKey) {
18
- ret[k] = obj[k];
19
- }
20
- return ret;
21
- }
22
-
23
- export async function deletePackageDependency(file: string, ...deps: string[]) {
24
- const original: any = await loadJsonFile(file);
25
- for (const k of ['devDependencies', 'dependencies']) {
26
- if (!original[k]) {
27
- continue;
28
- }
29
-
30
- let found = false;
31
- for (const name of deps) {
32
- if (original[k][name]) {
33
- delete original[k][name];
34
- found = true;
35
- }
36
- }
37
- if (found) {
38
- original[k] = sort(original[k]);
39
- }
40
- }
41
- await writeJsonFileBack(original);
42
- }
@@ -1,140 +0,0 @@
1
- import { stat, Stats } from 'fs';
2
- import { checkChildProcessResult, commandInPath } from '@idlebox/node';
3
- import { execa, Options as ExecaOptions } from 'execa';
4
-
5
- export interface PackageManagerConstructor {
6
- new (cwd: string): PackageManager;
7
- }
8
-
9
- export enum PackageManagerType {
10
- NPM,
11
- PNPM,
12
- RUSH,
13
- YARN,
14
- }
15
-
16
- export abstract class PackageManager {
17
- public abstract readonly friendlyName: string;
18
- public abstract readonly type: PackageManagerType;
19
- protected abstract readonly cliName: string;
20
- protected abstract readonly packageName: string;
21
- protected abstract readonly installCommand: string;
22
- protected abstract readonly installDevFlag: string;
23
- protected abstract readonly uninstallCommand: string;
24
- protected readonly runCommand: string = 'run';
25
- protected readonly initCommand: string = 'run';
26
- protected readonly showCommand: string = 'show';
27
- protected abstract readonly syncCommand: string;
28
-
29
- /** if set to true, debug info will print to stderr, default is process.stderr.isTTY */
30
- public displayBeforeCommandRun = process.stderr.isTTY;
31
-
32
- /** detect if this package manager is used by current project */
33
- public detect(): Promise<boolean> {
34
- return this._detect().catch((e) => {
35
- console.error('Exception of detect() package manager %s\n%s', this.friendlyName, e.stack);
36
- return false;
37
- });
38
- }
39
-
40
- protected abstract _detect(): Promise<boolean>;
41
-
42
- public constructor(protected readonly cwd: string) {}
43
-
44
- protected _detectFile(file: string) {
45
- return new Promise<boolean>((resolve) => {
46
- const wrappedCallback = (err: Error | null, data: Stats) => (err ? resolve(false) : resolve(!!data));
47
- stat(file, wrappedCallback);
48
- });
49
- }
50
-
51
- /** spawn package manager binary, with inherit stdio */
52
- public invokeCli(cmd: string, ...args: string[]): Promise<void> {
53
- const aa = [cmd, ...args].filter((v) => !!v);
54
- return this._invoke(this.cliName, aa);
55
- }
56
-
57
- /** spawn package manager binary, mute output */
58
- protected async _invokeErrorLater(
59
- cmd: string,
60
- args: string[],
61
- spawnOptions: Omit<ExecaOptions, 'stdio' | 'encoding'> = {}
62
- ): Promise<void> {
63
- const p = this.__invoke(cmd, args, {
64
- ...spawnOptions,
65
- stdio: ['ignore', 'pipe', 'pipe'],
66
- all: true,
67
- encoding: 'utf8',
68
- reject: false,
69
- });
70
- return p.then((ret) => {
71
- try {
72
- checkChildProcessResult(ret);
73
- } catch (e) {
74
- console.error(ret.all);
75
- throw e;
76
- }
77
- });
78
- }
79
-
80
- protected async _invoke(cmd: string, args: string[], spawnOptions: ExecaOptions = {}): Promise<void> {
81
- await this.__invoke(cmd, args, spawnOptions);
82
- }
83
-
84
- protected __invoke(cmd: string, args: string[], spawnOptions: ExecaOptions) {
85
- this.displayBeforeCommandRun && console.error('\x1B[38;5;14m%s %s\x1B[0m', cmd, args.join(' '));
86
- return execa(cmd, args, {
87
- stdio: 'inherit',
88
- cwd: this.cwd,
89
- reject: true,
90
- ...spawnOptions,
91
- });
92
- }
93
-
94
- /** run scripts in package.json, by package manager */
95
- public run(script: string, ...args: string[]) {
96
- return this.invokeCli(this.runCommand, script, ...args);
97
- }
98
-
99
- /** install packages
100
- * * add packages into package.json
101
- * * if "-D" or "--dev" in `packages`, add them to devDependencies
102
- **/
103
- public install(...packages: string[]) {
104
- const i1 = packages.indexOf('-D');
105
- if (i1 !== -1) {
106
- packages.splice(i1, 1);
107
- packages.unshift(this.installDevFlag);
108
- }
109
- const i2 = packages.indexOf('--dev');
110
- if (i2 !== -1) {
111
- packages.splice(i2, 1);
112
- packages.unshift(this.installDevFlag);
113
- }
114
- return this.invokeCli(this.installCommand, ...packages);
115
- }
116
-
117
- public uninstall(...packages: string[]) {
118
- return this.invokeCli(this.uninstallCommand, ...packages);
119
- }
120
-
121
- /** run package init command, normally this will create a new package.json, and maybe ask some questions */
122
- public init(...args: string[]) {
123
- return this.invokeCli(this.initCommand, ...args);
124
- }
125
-
126
- /** detect this package manager callable (installed and in PATH) */
127
- public exists() {
128
- return commandInPath(this.cliName);
129
- }
130
-
131
- /** sync package.json to node_modules, eg: npm i */
132
- public sync(...args: string[]) {
133
- return this.invokeCli(this.syncCommand, ...args);
134
- }
135
-
136
- /** show package info from NPM registry */
137
- public show(...args: string[]) {
138
- return this.invokeCli(this.showCommand, ...args);
139
- }
140
- }
@@ -1,5 +0,0 @@
1
- const { manifest } = require('pacote');
2
-
3
- export async function resolveLatestVersionOnNpm(packageName: string) {
4
- return '^' + (await manifest(packageName + '@latest')).version;
5
- }
package/src/index.ts DELETED
@@ -1,45 +0,0 @@
1
- import { getPackageManager } from './common/getPackageManager';
2
-
3
- function getArgs() {
4
- const args = process.argv.slice(2);
5
-
6
- const cmdAt = args.findIndex((e) => !e.startsWith('-'));
7
-
8
- if (cmdAt === -1) {
9
- return { cmd: '', args: [] };
10
- }
11
- const cmd = args.splice(cmdAt, 1)[0];
12
- return { cmd, args };
13
- }
14
-
15
- /** @internal */
16
- export default async function () {
17
- const { cmd, args } = getArgs();
18
-
19
- // console.error('finding package manager');
20
- const pm = await getPackageManager();
21
- // console.error('detected package manager: %s', pm.friendlyName);
22
- if (!cmd) {
23
- console.error('Usage: unpm <command> <...packages>');
24
- console.error(' * install, add, i, a - install package (can follow --dev)');
25
- console.error(' * uninstall, un, remove, rm, erase - remove package');
26
- console.error(' * run - run npm script');
27
- console.error(' * init - run init script');
28
- console.error(' * show, view - get and show package info from registry');
29
- console.error('other command: direct pass to package manager.');
30
- process.exit(1);
31
- }
32
- if (cmd === 'install' || cmd === 'add' || cmd === 'i' || cmd === 'a') {
33
- await pm.install(...args);
34
- } else if (cmd === 'uninstall' || cmd === 'un' || cmd === 'remove' || cmd === 'rm' || cmd === 'erase') {
35
- await pm.uninstall(...args);
36
- } else if (cmd === 'run' || cmd === 'r') {
37
- await pm.run(args[0], ...args.slice(1));
38
- } else if (cmd === 'init') {
39
- await pm.init(...args);
40
- } else if (cmd === 'show' || cmd === 'view') {
41
- await pm.show(...args);
42
- } else {
43
- await pm.invokeCli(cmd, ...args);
44
- }
45
- }
@@ -1,18 +0,0 @@
1
- import { PackageManagerType } from './../common/packageManager';
2
- import { PackageManager } from '../common/packageManager';
3
-
4
- /** @internal */
5
- export class Npm extends PackageManager {
6
- readonly type = PackageManagerType.NPM;
7
- readonly friendlyName: string = 'npm';
8
- readonly cliName: string = 'npm';
9
- readonly installCommand: string = 'install';
10
- readonly packageName: string = 'npm';
11
- readonly uninstallCommand: string = 'uninstall';
12
- readonly installDevFlag: string = '--save-dev';
13
- readonly syncCommand: string = 'i';
14
-
15
- _detect(): Promise<boolean> {
16
- return this._detectFile('package-lock.json');
17
- }
18
- }
@@ -1,17 +0,0 @@
1
- import { PackageManager, PackageManagerType } from '../common/packageManager';
2
-
3
- /** @internal */
4
- export class Pnpm extends PackageManager {
5
- readonly type = PackageManagerType.PNPM;
6
- readonly friendlyName: string = 'pnpm';
7
- readonly cliName: string = 'pnpm';
8
- readonly installCommand: string = 'add';
9
- readonly packageName: string = 'pnpm';
10
- readonly uninstallCommand: string = 'remove';
11
- readonly installDevFlag: string = '-D';
12
- readonly syncCommand: string = 'i';
13
-
14
- _detect(): Promise<boolean> {
15
- return this._detectFile('pnpm-lock.yaml');
16
- }
17
- }
@@ -1,118 +0,0 @@
1
- import { readFile as readFileAsync } from 'fs';
2
- import { dirname, relative, resolve } from 'path';
3
- import { promisify } from 'util';
4
- import { findUpUntil } from '@idlebox/node';
5
- import { loadJsonFile } from '@idlebox/node-json-edit';
6
- import { parse } from 'json5';
7
- import { deletePackageDependency, resortPackage } from '../common/packageJson';
8
- import { PackageManager, PackageManagerType } from '../common/packageManager';
9
-
10
- const readFile = promisify(readFileAsync);
11
- const subCommands = ['run', 'init', 'show', 'view'];
12
-
13
- /** @internal */
14
- export class Rush extends PackageManager {
15
- readonly type = PackageManagerType.RUSH;
16
- readonly friendlyName: string = 'rush';
17
- readonly cliName: string = 'rush';
18
- readonly installCommand: string = 'add';
19
- readonly packageName: string = '@microsoft/rush';
20
- readonly uninstallCommand: string = '!!';
21
- readonly installDevFlag: string = '--dev';
22
- readonly syncCommand: string = 'update';
23
- showCommand = '';
24
-
25
- private rushRoot?: string;
26
- private subPackageManager?: string;
27
-
28
- async _detect(sub = false): Promise<boolean> {
29
- const found = await findUpUntil(this.cwd, 'rush.json');
30
- if (!found) {
31
- return false;
32
- }
33
- this.rushRoot = dirname(found);
34
- const data = parse(await readFile(found, 'utf-8'));
35
- let pm = '';
36
- for (const key of ['pnpm', 'npm', 'yarn']) {
37
- if (data[key + 'Version']) {
38
- pm = key;
39
- break;
40
- }
41
- }
42
- if (pm) {
43
- if (pm === 'pnpm') {
44
- this.showCommand = 'view';
45
- }
46
-
47
- this.subPackageManager = resolve(this.rushRoot, 'common/temp', `${pm}-local`, 'node_modules/.bin', pm);
48
- } else {
49
- if (sub) {
50
- throw new Error('can not determine sub package manager of rush');
51
- }
52
- this.subPackageManager = '';
53
- console.warn('Warn: can not determine sub package manager of rush.');
54
- }
55
-
56
- return true;
57
- }
58
-
59
- public async uninstall(...packages: string[]): Promise<void> {
60
- const pkgJson = await findUpUntil(this.cwd, 'package.json');
61
- if (pkgJson) {
62
- await deletePackageDependency(pkgJson, ...packages);
63
- }
64
- await super._invoke(this.cliName, ['update']);
65
- }
66
-
67
- async install(..._packages: string[]) {
68
- const packages = _packages.filter((item) => !item.startsWith('-'));
69
- const flags = ['--caret', '--skip-update', '--make-consistent'];
70
- if (_packages.includes('-D') || _packages.includes('--dev')) {
71
- flags.push('--dev');
72
- }
73
-
74
- for (const pkg of packages) {
75
- await super._invokeErrorLater(this.cliName, [this.installCommand, ...flags, '-p', pkg]);
76
- }
77
-
78
- const pkgJson = await findUpUntil(this.cwd, 'package.json');
79
- if (pkgJson) {
80
- await resortPackage(pkgJson);
81
- }
82
-
83
- await super._invoke(this.cliName, ['update']);
84
- }
85
-
86
- async init() {
87
- await super.init();
88
-
89
- const data = await loadJsonFile(resolve(this.rushRoot!, 'rush.json'));
90
- const pkg = require(resolve(this.cwd, 'package.json'));
91
-
92
- const alreadyExists = data.projects.some(({ packageName }: any) => {
93
- return packageName === pkg.name;
94
- });
95
- if (alreadyExists) {
96
- return;
97
- }
98
-
99
- data.projects.push({
100
- packageName: pkg.name,
101
- projectFolder: relative(this.rushRoot!, this.cwd),
102
- shouldPublish: !pkg.private,
103
- });
104
- }
105
-
106
- public async invokeCli(cmd: string, ...args: string[]): Promise<void> {
107
- if (subCommands.includes(cmd)) {
108
- if (!this.subPackageManager) {
109
- await this._detect(true);
110
- }
111
-
112
- const aa = [cmd, ...args].filter((v) => !!v);
113
- return this._invoke(this.subPackageManager!, aa);
114
- } else {
115
- return super.invokeCli(cmd, ...args);
116
- }
117
- }
118
- }
@@ -1,18 +0,0 @@
1
- import { PackageManager, PackageManagerType } from '../common/packageManager';
2
-
3
- /** @internal */
4
- export class Yarn extends PackageManager {
5
- readonly type = PackageManagerType.YARN;
6
- readonly friendlyName: string = 'yarn';
7
- readonly cliName: string = 'yarn';
8
- readonly installCommand: string = 'add';
9
- readonly packageName: string = 'yarn';
10
- readonly uninstallCommand: string = 'remove';
11
- readonly installDevFlag: string = '--dev';
12
- readonly syncCommand: string = 'install';
13
- readonly showCommand: string = 'view';
14
-
15
- _detect(): Promise<boolean> {
16
- return this._detectFile('yarn.lock');
17
- }
18
- }
package/src/tsconfig.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../node_modules/@build-script/single-dog-asset/package/tsconfig.json",
3
- "compilerOptions": {
4
- "outDir": "../lib",
5
- "rootDir": ".",
6
- "typeRoots": ["../node_modules/@types"],
7
- "declaration": false,
8
- "declarationMap": false
9
- }
10
- }