@rife/cli 0.0.6-beta.8 → 0.0.6-beta.9

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.
@@ -1,115 +1,115 @@
1
- import type { ChildProcess } from 'child_process';
2
-
3
- import { AsyncSeriesHook } from 'tapable';
4
-
5
- import { TsxConfig, checkAndEmitType, compileTs, formatDiagnostic } from './compiler';
6
- import type { Plugin } from '../../runner';
7
-
8
- declare module '../../runner' {
9
- interface Runner {
10
- compiler: {
11
- fail: boolean;
12
- process: ChildProcess | null;
13
- hook: {
14
- checkAndEmitType: AsyncSeriesHook<[]>;
15
- execMain: AsyncSeriesHook<[]>;
16
- };
17
- tsx: TsxConfig;
18
- };
19
- }
20
- }
21
-
22
- export interface pluginCompilerOptions {
23
- tsx?: Partial<TsxConfig>;
24
- less?: {};
25
- }
26
-
27
- export function pluginCompiler(options?: pluginCompilerOptions) {
28
- const plugin: Plugin = {
29
- name: pluginCompiler.name,
30
- apply: runner => {
31
- const { hook, logger, z, tapable, fs } = runner;
32
- const log = logger.withTag(pluginCompiler.name);
33
- const validation = z
34
- .object({
35
- tsx: z
36
- .object({
37
- type: z.enum(['tsc', 'swc']).default('swc'),
38
- rootDir: z.string().trim().min(1).default('.'),
39
- outDir: z.string().trim().min(1).default('./dist/'),
40
- clean: z.boolean().default(true),
41
- main: z.string().trim().optional(),
42
- options: z.object({}).passthrough().optional(),
43
- })
44
- .default({}),
45
- })
46
- .optional()
47
- .default({})
48
- .safeParse(options);
49
-
50
- if (!validation.success) {
51
- log.error('配置错误\n', validation.error.message);
52
- process.exit(1);
53
- }
54
-
55
- runner.compiler = {
56
- fail: false,
57
- process: null,
58
- hook: {
59
- checkAndEmitType: new tapable.AsyncSeriesHook(),
60
- execMain: new tapable.AsyncSeriesHook(),
61
- },
62
- tsx: validation.data.tsx,
63
- };
64
-
65
- hook.dev.tapPromise(pluginCompiler.name, async () => {
66
- const tsxConfig: TsxConfig = validation.data.tsx;
67
- compileTs(runner, tsxConfig, { dev: true });
68
- });
69
-
70
- hook.build.tapPromise(pluginCompiler.name, async () => {
71
- const perfStart = performance.now();
72
- const tsxConfig: TsxConfig = validation.data.tsx;
73
- await compileTs(runner, tsxConfig, { build: true });
74
- const duration = ((performance.now() - perfStart) / 1000).toFixed(3);
75
- if (runner.compiler.fail) {
76
- log.fail(`构建失败,耗时 ${duration} s`);
77
- process.exit(1);
78
- } else {
79
- log.success(`构建成功,耗时 ${duration} s`);
80
- }
81
- });
82
-
83
- runner.compiler.hook.checkAndEmitType.tapPromise(pluginCompiler.name, async () => {
84
- const { diagnostics, duration } = checkAndEmitType();
85
- if (diagnostics.length) {
86
- log.error(`类型检查失败,耗时 ${duration} s\n\n%s`, formatDiagnostic(diagnostics));
87
- runner.compiler.fail = true;
88
- } else {
89
- log.success(`类型检查成功,耗时 ${duration} s`);
90
- }
91
- });
92
-
93
- runner.compiler.hook.execMain.tapPromise(pluginCompiler.name, async () => {
94
- const compiler = runner.compiler;
95
- const { main } = compiler.tsx;
96
- if (!main) {
97
- log.debug('没有指定 main');
98
- return;
99
- }
100
-
101
- if (!(await fs.exists(main))) {
102
- log.warn(`启动文件不存在 %s`, main);
103
- return;
104
- }
105
-
106
- log.info(`启动文件 node %s`, main);
107
-
108
- const { fork } = require('child_process') as typeof import('child_process');
109
- compiler.process?.kill();
110
- compiler.process = fork(main, { stdio: 'inherit' });
111
- });
112
- },
113
- };
114
- return plugin;
115
- }
1
+ import type { ChildProcess } from 'child_process';
2
+
3
+ import { AsyncSeriesHook } from 'tapable';
4
+
5
+ import { TsxConfig, checkAndEmitType, compileTs, formatDiagnostic } from './compiler';
6
+ import type { Plugin } from '../../runner';
7
+ import { getDuration } from '../../util';
8
+
9
+ declare module '../../runner' {
10
+ interface Runner {
11
+ compiler: {
12
+ fail: boolean;
13
+ process: ChildProcess | null;
14
+ hook: {
15
+ checkAndEmitType: AsyncSeriesHook<[]>;
16
+ execMain: AsyncSeriesHook<[]>;
17
+ };
18
+ tsx: TsxConfig;
19
+ };
20
+ }
21
+ }
22
+
23
+ export interface pluginCompilerOptions {
24
+ tsx?: Partial<TsxConfig>;
25
+ less?: {};
26
+ }
27
+
28
+ export function pluginCompiler(options?: pluginCompilerOptions) {
29
+ const plugin: Plugin = {
30
+ name: pluginCompiler.name,
31
+ apply: runner => {
32
+ const { hook, logger, z, tapable, fs } = runner;
33
+ const log = logger.withTag(pluginCompiler.name);
34
+ const validation = z
35
+ .object({
36
+ tsx: z
37
+ .object({
38
+ type: z.enum(['tsc', 'swc']).default('swc'),
39
+ rootDir: z.string().trim().min(1).default('.'),
40
+ outDir: z.string().trim().min(1).default('./dist/'),
41
+ clean: z.boolean().default(true),
42
+ main: z.string().trim().optional(),
43
+ options: z.object({}).passthrough().optional(),
44
+ })
45
+ .default({}),
46
+ })
47
+ .optional()
48
+ .default({})
49
+ .safeParse(options);
50
+
51
+ if (!validation.success) {
52
+ log.error('配置错误\n', validation.error.message);
53
+ process.exit(1);
54
+ }
55
+
56
+ runner.compiler = {
57
+ fail: false,
58
+ process: null,
59
+ hook: {
60
+ checkAndEmitType: new tapable.AsyncSeriesHook(),
61
+ execMain: new tapable.AsyncSeriesHook(),
62
+ },
63
+ tsx: validation.data.tsx,
64
+ };
65
+
66
+ hook.dev.tapPromise(pluginCompiler.name, async () => {
67
+ const tsxConfig: TsxConfig = validation.data.tsx;
68
+ compileTs(runner, tsxConfig, { dev: true });
69
+ });
70
+
71
+ hook.build.tapPromise(pluginCompiler.name, async () => {
72
+ const perfStart = performance.now();
73
+ const tsxConfig: TsxConfig = validation.data.tsx;
74
+ await compileTs(runner, tsxConfig, { build: true });
75
+ if (runner.compiler.fail) {
76
+ log.fail(`构建失败,${getDuration(perfStart)}`);
77
+ process.exit(1);
78
+ } else {
79
+ log.success(`构建成功,${getDuration(perfStart)}`);
80
+ }
81
+ });
82
+
83
+ runner.compiler.hook.checkAndEmitType.tapPromise(pluginCompiler.name, async () => {
84
+ const { diagnostics, duration } = checkAndEmitType();
85
+ if (diagnostics.length) {
86
+ log.error(`类型检查失败,耗时 ${duration} s\n\n%s`, formatDiagnostic(diagnostics));
87
+ runner.compiler.fail = true;
88
+ } else {
89
+ log.success(`类型检查成功,耗时 ${duration} s`);
90
+ }
91
+ });
92
+
93
+ runner.compiler.hook.execMain.tapPromise(pluginCompiler.name, async () => {
94
+ const compiler = runner.compiler;
95
+ const { main } = compiler.tsx;
96
+ if (!main) {
97
+ log.debug('没有指定 main');
98
+ return;
99
+ }
100
+
101
+ if (!(await fs.exists(main))) {
102
+ log.warn(`启动文件不存在 %s`, main);
103
+ return;
104
+ }
105
+
106
+ log.info(`启动文件 node %s`, main);
107
+
108
+ const { fork } = require('child_process') as typeof import('child_process');
109
+ compiler.process?.kill();
110
+ compiler.process = fork(main, { stdio: 'inherit' });
111
+ });
112
+ },
113
+ };
114
+ return plugin;
115
+ }
@@ -1,64 +1,78 @@
1
- import path from 'path';
2
-
3
- import type { Config, Plugin } from '../runner';
4
-
5
- export function pluginConfig() {
6
- const plugin: Plugin = {
7
- name: pluginConfig.name,
8
- apply: runner => {
9
- const { logger, hook, config, command, fs, z } = runner;
10
- const log = logger.withTag(pluginConfig.name);
11
-
12
- hook.loadConfig.tapPromise(pluginConfig.name, async () => {
13
- const fileName = '.app.js';
14
- const filePath = path.posix.resolve(fileName);
15
- log.debug(`filePath %s`, filePath);
16
- if (!fs.existsSync(filePath)) {
17
- log.error(`找不到配置文件 ${fileName}`);
18
- process.exit(1);
19
- }
20
- const all: Config[] = require(filePath);
21
- runner.config.all = all;
22
- });
23
-
24
- hook.validateConfig.tapPromise(pluginConfig.name, async () => {
25
- const validation = z
26
- .array(
27
- z.object({
28
- name: z.string().trim().min(1),
29
- }),
30
- )
31
- .min(1)
32
- .safeParse(config.all);
33
- if (!validation.success) {
34
- log.error(validation.error.message);
35
- process.exit(1);
36
- }
37
- });
38
-
39
- hook.applyConfig.tapPromise(pluginConfig.name, async () => {
40
- const result = z
41
- .object({
42
- name: z.string().trim().default(''),
43
- })
44
- .default({})
45
- .safeParse(command.opts());
46
- const name = result.data?.name;
47
- if (!name) {
48
- log.debug('没有指定 --name,默认使用列表第一个');
49
- config.current = config.all[0];
50
- } else {
51
- log.debug(`指定 --name ${name}`);
52
- const find = config.all.find(t => t.name === name);
53
- if (!find) {
54
- log.error(`未找到 name '%s'`, name);
55
- process.exit(1);
56
- }
57
- config.current = find;
58
- }
59
- config.current.plugins.forEach(plugin => plugin.apply(runner));
60
- });
61
- },
62
- };
63
- return plugin;
64
- }
1
+ import path from 'path';
2
+
3
+ import type { Config, Plugin } from '../runner';
4
+
5
+ export function pluginConfig() {
6
+ const plugin: Plugin = {
7
+ name: pluginConfig.name,
8
+ apply: runner => {
9
+ const { logger, hook, config, command, fs, z } = runner;
10
+ const log = logger.withTag(pluginConfig.name);
11
+
12
+ hook.loadConfig.tapPromise(pluginConfig.name, async () => {
13
+ let filePath = '';
14
+ const mjs = 'app.mjs';
15
+ const fileNames = ['app.js', 'app.cjs', mjs];
16
+ let isEsm = false;
17
+ for (const fileName of fileNames) {
18
+ if (await fs.exists(fileName)) {
19
+ filePath = path.posix.resolve(fileName);
20
+ isEsm = fileName === mjs;
21
+ break;
22
+ }
23
+ }
24
+ log.debug(`filePath %s`, filePath);
25
+ if (!filePath) {
26
+ log.error(`找不到配置文件 app.(c|m)?js`);
27
+ process.exit(1);
28
+ }
29
+ let all: Config[] = [];
30
+ if (isEsm) {
31
+ all = (await import(filePath)).default;
32
+ } else {
33
+ all = require(filePath);
34
+ }
35
+ runner.config.all = all;
36
+ });
37
+
38
+ hook.validateConfig.tapPromise(pluginConfig.name, async () => {
39
+ const validation = z
40
+ .array(
41
+ z.object({
42
+ name: z.string().trim().min(1),
43
+ }),
44
+ )
45
+ .min(1)
46
+ .safeParse(config.all);
47
+ if (!validation.success) {
48
+ log.error(validation.error.message);
49
+ process.exit(1);
50
+ }
51
+ });
52
+
53
+ hook.applyConfig.tapPromise(pluginConfig.name, async () => {
54
+ const result = z
55
+ .object({
56
+ name: z.string().trim().default(''),
57
+ })
58
+ .default({})
59
+ .safeParse(command.opts());
60
+ const name = result.data?.name;
61
+ if (!name) {
62
+ log.debug('没有指定 --name,默认使用列表第一个');
63
+ config.current = config.all[0];
64
+ } else {
65
+ log.debug(`指定 --name ${name}`);
66
+ const find = config.all.find(t => t.name === name);
67
+ if (!find) {
68
+ log.error(`未找到 name '%s'`, name);
69
+ process.exit(1);
70
+ }
71
+ config.current = find;
72
+ }
73
+ config.current.plugins.forEach(plugin => plugin.apply(runner));
74
+ });
75
+ },
76
+ };
77
+ return plugin;
78
+ }
@@ -2,3 +2,4 @@ export * from './config';
2
2
  export * from './package';
3
3
  export * from './commander';
4
4
  export * from './compiler';
5
+ export * from './release';
@@ -1,4 +1,4 @@
1
- import { exec, execSync } from 'child_process';
1
+ import { execSync } from 'child_process';
2
2
 
3
3
  import type { Plugin } from '../runner';
4
4
 
package/src/runner.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import type { ConsolaInstance } from 'consola';
2
- import * as glob from 'glob';
2
+ import { colors } from 'consola/utils';
3
3
  import { AsyncSeriesHook } from 'tapable';
4
4
  import zod from 'zod';
5
5
 
6
6
  import { createLogger } from './logger';
7
+ import { fs, glob } from './util';
7
8
 
8
9
  export interface Config {
9
10
  name: string;
@@ -37,10 +38,11 @@ export interface Runner {
37
38
  package: any;
38
39
  z: typeof zod;
39
40
  fs: typeof import('fs-extra');
40
- glob: (typeof import('glob'))['glob'];
41
+ glob: typeof import('glob')['glob'];
41
42
  tapable: typeof import('tapable');
42
43
  duration: string;
43
44
  tempDir: string;
45
+ clear: () => void;
44
46
  }
45
47
 
46
48
  export interface Plugin {
@@ -105,7 +107,7 @@ export function createRunner() {
105
107
  },
106
108
  package: {},
107
109
  z: zod,
108
- fs: require('fs-extra') as typeof import('fs-extra'),
110
+ fs,
109
111
  glob: glob.glob,
110
112
  tapable: require('tapable') as typeof import('tapable'),
111
113
  get duration() {
@@ -113,6 +115,11 @@ export function createRunner() {
113
115
  return `耗时 ${duration} s`;
114
116
  },
115
117
  tempDir: './node_modules/.temp/',
118
+ clear: () => {
119
+ console.clear();
120
+ process.stdout.write('\x1b[3J');
121
+ console.log(`${colors.bgBlue(' 项目名称 ')} ${runner.package.name} \n`);
122
+ },
116
123
  };
117
124
 
118
125
  return runner;
package/src/sync.ts CHANGED
@@ -26,12 +26,12 @@ const list = [
26
26
  '@rife/config', //
27
27
  '@rife/cli', //
28
28
  // '@rife/infra',
29
- '@rife/logger',
29
+ // '@rife/logger',
30
30
  // '@rife/react',
31
31
  // '@rife/next',
32
32
  // '@rife/nest',
33
- '@rife/plugin-deploy',
34
- '@rife/fast',
33
+ // '@rife/plugin-deploy',
34
+ // '@rife/fast',
35
35
  ];
36
36
 
37
37
  async function main() {
package/src/util.ts ADDED
@@ -0,0 +1,9 @@
1
+ import fs from 'fs-extra';
2
+ import * as glob from 'glob';
3
+
4
+ export { fs, glob };
5
+
6
+ export const getDuration = (perfStart: number) => {
7
+ const duration = ((performance.now() - perfStart) / 1000).toFixed(3);
8
+ return `耗时 ${duration} s`;
9
+ };
package/src/pnpmfile.ts DELETED
@@ -1,147 +0,0 @@
1
- // @ts-ignore
2
- const fs = require('fs');
3
- // @ts-ignore
4
- const h = require('https');
5
-
6
- const request = h.request;
7
- h.request = function (...args: any[]) {
8
- const path = args[0].path || '';
9
- if (path.startsWith('/@rife') && !path.endsWith('.tgz')) {
10
- console.log(path);
11
- args[0].headers['accept'] = 'application/json';
12
- }
13
- return request(...args);
14
- };
15
-
16
- let root: any;
17
- let exit: (message: string) => any;
18
- const origin: any = {};
19
-
20
- export function parseVersion(version: string) {
21
- const [_, a = '', b = '', c = '', d = ''] = /(\d+?)\.(\d+?)\.(\d+?)\D*(\d+)?/.exec(version) || [];
22
- if (!a || !b || !c) {
23
- exit(`version ${version}`);
24
- }
25
- return [a, b, c, d].map(t => t.padStart(3, '0')).join('.');
26
- }
27
-
28
- function compareVersion(v1: string, v2: string) {
29
- const v = [v1, v2].filter(t => Boolean(t));
30
- if (v.length === 0) {
31
- exit('version empty');
32
- }
33
- if (v.length === 1) {
34
- return v[0];
35
- }
36
-
37
- return parseVersion(v1) > parseVersion(v2) ? v1 : v2;
38
- }
39
-
40
- const host: any = {};
41
- function updateRoot(type: 'dependencies' | 'devDependencies', name: string, version: string, from: string) {
42
- if (origin[type][name]) return;
43
- if (!host[type]) {
44
- host[type] = {};
45
- }
46
- const dependencies = host[type];
47
- dependencies[name] = {
48
- version: compareVersion(version, dependencies[name]?.version),
49
- from,
50
- };
51
- }
52
-
53
- export const readPackage = (pkg: any, context: any) => {
54
- if (!exit) {
55
- exit = (message: string) => {
56
- context.log(message);
57
- // @ts-ignore
58
- process.exit(1);
59
- };
60
- }
61
- if (!root) {
62
- root = pkg;
63
- origin.dependencies = { ...root.dependencies };
64
- origin.devDependencies = { ...root.devDependencies };
65
- }
66
- const dependencies = pkg.dependencies || {};
67
- const devDependencies = pkg.devDependencies || {};
68
- const { hostDependencies = {} } = pkg.rife || {};
69
- const from = {
70
- name: pkg.name,
71
- version: pkg.version,
72
- };
73
- Object.keys(hostDependencies).forEach(name => {
74
- const version = dependencies[name] || devDependencies[name];
75
- if (!version) {
76
- exit('cannot not found version');
77
- }
78
- const isDev = Boolean(devDependencies[name]);
79
- if (isDev) {
80
- dependencies[name] = version;
81
- updateRoot('devDependencies', name, version, from);
82
- } else {
83
- updateRoot('dependencies', name, version, from);
84
- }
85
- });
86
- return pkg;
87
- };
88
-
89
- const deps = ['devDependencies', 'dependencies'];
90
- const need: any[] = [];
91
-
92
- let aaaa = [];
93
-
94
- export const afterAllResolved = (lockfile, context) => {
95
- fs.writeFileSync('./temp/1.json', JSON.stringify(host, undefined, 4));
96
- debugger;
97
-
98
- deps.map(dep => {
99
- Object.entries(host[dep as any]).forEach(([name, data]: any) => {
100
- need.push({
101
- key: `${name}@${data.version}`,
102
- name,
103
- dep,
104
- version: data.version,
105
- from: data.from,
106
- source: `${data.from.name}@${data.from.version}`,
107
- });
108
- });
109
- });
110
-
111
- // debugger;
112
- const current = lockfile.importers['.'];
113
- const { packages } = lockfile;
114
-
115
- const list = Object.keys(packages).map(key => {
116
- const pack = {
117
- key,
118
- value: packages[key],
119
- };
120
- return pack;
121
- });
122
- const t = Object.keys(packages).filter(t => t.startsWith('@rife/cli'));
123
- aaaa = need.map(n => {
124
- const f = list.find(t => t.key.startsWith(n.key));
125
- if (f) {
126
- return f;
127
- }
128
- const s = list.find(t2 => t2.key.startsWith(n.source));
129
- const v = s?.value?.dependencies[n.name];
130
- if (v) {
131
- const newKey = `${n.name}@${v}`;
132
- const ff = list.find(t => t.key.startsWith(newKey));
133
- if (ff) {
134
- return ff;
135
- }
136
- }
137
- debugger;
138
- });
139
-
140
- debugger;
141
- return lockfile;
142
- };
143
-
144
- process.on('exit', () => {
145
- // const exits = aaaa.map(a => fs.exitsSync(`./node_modules/.pnpm/${a.replace('/', '_')}`));
146
- // debugger;
147
- });