@modern-js/generator-utils 1.0.0-alpha.3

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/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # @modern-js/generator-utils
2
+
3
+ ## 1.0.0-alpha.3
4
+ ### Patch Changes
5
+
6
+ - feat: initial
7
+ - Updated dependencies [undefined]
8
+ - @modern-js/plugin-i18n@1.0.0-alpha.3
9
+ - @modern-js/utils@1.0.0-alpha.3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Modern.js
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,32 @@
1
+
2
+ <p align="center">
3
+ <a href="https://modernjs.dev" target="blank"><img src="https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png" width="300" alt="Modern.js Logo" /></a>
4
+ </p>
5
+
6
+ <p align="center">现代 Web 工程体系</p>
7
+
8
+ ## 背景
9
+ - [迈入现代 Web 开发](https://zhuanlan.zhihu.com/p/386607009)
10
+ - [现代 Web 开发者问卷调查报告](https://zhuanlan.zhihu.com/p/403206195)
11
+
12
+ ## 计划
13
+
14
+ Modern.js 的 1.0.0.rc 版已经发到 npm,目前在做测试改进,README 文档之后统一提供(现阶段加入测试和开发,可以发 [issue](https://github.com/modern-js-dev/modern.js/issues) 留微信联系),完整的文档站计划在10月14日上线
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
28
+
29
+
30
+
31
+
32
+
@@ -0,0 +1,152 @@
1
+ import path from 'path';
2
+ import os from 'os';
3
+ import execa from 'execa';
4
+ import ora from 'ora';
5
+ import { fs, getMonorepoPackages } from '@modern-js/utils';
6
+ import { canUseNpm, canUsePnpm, canUseYarn } from "./utils/env";
7
+ import { stripAnsi } from "./utils/strip-ansi";
8
+ import { i18n, localeKeys } from "./locale";
9
+ export * from "./utils";
10
+ export { fs } from '@modern-js/utils';
11
+ export { i18n } from "./locale"; // eslint-disable-next-line max-statements
12
+
13
+ export async function getPackageVersion(packageName, registry) {
14
+ const spinner = ora('Loading...').start();
15
+ spinner.color = 'yellow';
16
+
17
+ if (await canUsePnpm()) {
18
+ const args = ['info', packageName, 'version'];
19
+
20
+ if (registry) {
21
+ args.push(`--registry=${registry}`);
22
+ }
23
+
24
+ const result = await execa('pnpm', args);
25
+ spinner.stop();
26
+ return stripAnsi(result.stdout);
27
+ }
28
+
29
+ if (await canUseYarn()) {
30
+ const args = ['info', packageName, 'version', '--silent'];
31
+
32
+ if (registry) {
33
+ args.push(`--registry=${registry}`);
34
+ }
35
+
36
+ const result = await execa('yarn', args);
37
+ spinner.stop();
38
+ return stripAnsi(result.stdout);
39
+ }
40
+
41
+ if (await canUseNpm()) {
42
+ const args = ['view', packageName, 'version'];
43
+
44
+ if (registry) {
45
+ args.push(`--registry=${registry}`);
46
+ }
47
+
48
+ const result = await execa('npm', args);
49
+ spinner.stop();
50
+ return stripAnsi(result.stdout);
51
+ }
52
+
53
+ spinner.stop();
54
+ throw new Error('not found npm, please install npm before');
55
+ }
56
+ export function getPackageManager(cwd = process.cwd()) {
57
+ let appDirectory = cwd;
58
+
59
+ while (os.homedir() !== appDirectory) {
60
+ if (fs.existsSync(path.resolve(appDirectory, 'pnpm-lock.yaml'))) {
61
+ return 'pnpm';
62
+ }
63
+
64
+ if (fs.existsSync(path.resolve(appDirectory, 'yarn.lock'))) {
65
+ return 'yarn';
66
+ }
67
+
68
+ if (fs.existsSync(path.resolve(appDirectory, 'package-lock.json'))) {
69
+ return 'npm';
70
+ }
71
+
72
+ appDirectory = path.join(appDirectory, '..');
73
+ }
74
+
75
+ return 'npm';
76
+ }
77
+ export function isTsProject(appDir) {
78
+ return fs.existsSync(path.join(appDir, 'tsconfig.json'));
79
+ }
80
+ export async function getPackageObj(context) {
81
+ const pkgStr = (await context.materials.default.get(`package.json`).value()).content;
82
+ return JSON.parse(pkgStr);
83
+ }
84
+ export function getAllPackages(appDir) {
85
+ const packages = getMonorepoPackages(appDir);
86
+ return packages.map(pkg => pkg.name);
87
+ }
88
+ export function validatePackageName(value, packages, options) {
89
+ const {
90
+ isMonorepoSubProject
91
+ } = options;
92
+
93
+ if (isMonorepoSubProject && packages.includes(value)) {
94
+ return {
95
+ success: false,
96
+ error: i18n.t(localeKeys.packageName.exit, {
97
+ value
98
+ })
99
+ };
100
+ }
101
+
102
+ return {
103
+ success: true
104
+ };
105
+ }
106
+ export function validatePackagePath(value, projectDir, options) {
107
+ const {
108
+ isMwa,
109
+ isPublic,
110
+ isTest
111
+ } = options || {};
112
+ let dir = 'apps';
113
+
114
+ if (isMwa && isTest) {
115
+ dir = 'examples';
116
+ } else {
117
+ dir = isPublic ? 'packages' : 'features';
118
+ }
119
+
120
+ const packageDir = path.resolve(projectDir || '', dir, value);
121
+
122
+ if (fs.existsSync(packageDir)) {
123
+ return {
124
+ success: false,
125
+ error: i18n.t(localeKeys.pacakgePath.exit, {
126
+ value
127
+ })
128
+ };
129
+ }
130
+
131
+ return {
132
+ success: true
133
+ };
134
+ }
135
+ export function getModuleProjectPath(packagePath, isMonorepoSubProject, isPublic, isLocalPackages) {
136
+ if (isLocalPackages && packagePath) {
137
+ return `${packagePath}/`;
138
+ }
139
+
140
+ if (isMonorepoSubProject && packagePath) {
141
+ return `${isPublic ? 'packages' : 'features'}/${packagePath}/`;
142
+ }
143
+
144
+ return '';
145
+ }
146
+ export function getMWAProjectPath(packagePath, isMonorepoSubProject, isTest = false) {
147
+ if (isMonorepoSubProject && packagePath) {
148
+ return `${isTest ? 'examples' : 'apps'}/${packagePath}/`;
149
+ }
150
+
151
+ return '';
152
+ }
@@ -0,0 +1,8 @@
1
+ export const EN_LOCALE = {
2
+ packageName: {
3
+ exit: 'package name `{value}` is already exists'
4
+ },
5
+ pacakgePath: {
6
+ exit: 'package path {value} is already exists'
7
+ }
8
+ };
@@ -0,0 +1,9 @@
1
+ import { I18n } from '@modern-js/plugin-i18n';
2
+ import { ZH_LOCALE } from "./zh";
3
+ import { EN_LOCALE } from "./en";
4
+ const i18n = new I18n();
5
+ const localeKeys = i18n.init('zh', {
6
+ zh: ZH_LOCALE,
7
+ en: EN_LOCALE
8
+ });
9
+ export { i18n, localeKeys };
@@ -0,0 +1,8 @@
1
+ export const ZH_LOCALE = {
2
+ packageName: {
3
+ exit: '项目名称 {value} 已存在'
4
+ },
5
+ pacakgePath: {
6
+ exit: '目录 {value} 已存在'
7
+ }
8
+ };
@@ -0,0 +1,31 @@
1
+ import execa from 'execa';
2
+ export async function canUseNpm() {
3
+ try {
4
+ await execa('npm', ['--version'], {
5
+ env: process.env
6
+ });
7
+ return true;
8
+ } catch (e) {
9
+ return false;
10
+ }
11
+ }
12
+ export async function canUseYarn() {
13
+ try {
14
+ await execa('yarn', ['--version'], {
15
+ env: process.env
16
+ });
17
+ return true;
18
+ } catch (e) {
19
+ return false;
20
+ }
21
+ }
22
+ export async function canUsePnpm() {
23
+ try {
24
+ await execa('pnpm', ['--version'], {
25
+ env: process.env
26
+ });
27
+ return true;
28
+ } catch (e) {
29
+ return false;
30
+ }
31
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./env";
2
+ export * from "./strip-ansi";
@@ -0,0 +1,14 @@
1
+ function ansiRegex({
2
+ onlyFirst = false
3
+ } = {}) {
4
+ const pattern = ['[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'].join('|');
5
+ return new RegExp(pattern, onlyFirst ? undefined : 'g');
6
+ }
7
+
8
+ export function stripAnsi(string) {
9
+ if (typeof string !== 'string') {
10
+ throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
11
+ }
12
+
13
+ return string.replace(ansiRegex(), '');
14
+ }
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ getPackageVersion: true,
8
+ getPackageManager: true,
9
+ isTsProject: true,
10
+ getPackageObj: true,
11
+ getAllPackages: true,
12
+ validatePackageName: true,
13
+ validatePackagePath: true,
14
+ getModuleProjectPath: true,
15
+ getMWAProjectPath: true,
16
+ fs: true,
17
+ i18n: true
18
+ };
19
+ Object.defineProperty(exports, "fs", {
20
+ enumerable: true,
21
+ get: function () {
22
+ return _utils.fs;
23
+ }
24
+ });
25
+ exports.getAllPackages = getAllPackages;
26
+ exports.getMWAProjectPath = getMWAProjectPath;
27
+ exports.getModuleProjectPath = getModuleProjectPath;
28
+ exports.getPackageManager = getPackageManager;
29
+ exports.getPackageObj = getPackageObj;
30
+ exports.getPackageVersion = getPackageVersion;
31
+ Object.defineProperty(exports, "i18n", {
32
+ enumerable: true,
33
+ get: function () {
34
+ return _locale.i18n;
35
+ }
36
+ });
37
+ exports.isTsProject = isTsProject;
38
+ exports.validatePackageName = validatePackageName;
39
+ exports.validatePackagePath = validatePackagePath;
40
+
41
+ var _path = _interopRequireDefault(require("path"));
42
+
43
+ var _os = _interopRequireDefault(require("os"));
44
+
45
+ var _execa = _interopRequireDefault(require("execa"));
46
+
47
+ var _ora = _interopRequireDefault(require("ora"));
48
+
49
+ var _utils = require("@modern-js/utils");
50
+
51
+ var _env = require("./utils/env");
52
+
53
+ var _stripAnsi = require("./utils/strip-ansi");
54
+
55
+ var _locale = require("./locale");
56
+
57
+ var _utils2 = require("./utils");
58
+
59
+ Object.keys(_utils2).forEach(function (key) {
60
+ if (key === "default" || key === "__esModule") return;
61
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
62
+ if (key in exports && exports[key] === _utils2[key]) return;
63
+ Object.defineProperty(exports, key, {
64
+ enumerable: true,
65
+ get: function () {
66
+ return _utils2[key];
67
+ }
68
+ });
69
+ });
70
+
71
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
72
+
73
+ // eslint-disable-next-line max-statements
74
+ async function getPackageVersion(packageName, registry) {
75
+ const spinner = (0, _ora.default)('Loading...').start();
76
+ spinner.color = 'yellow';
77
+
78
+ if (await (0, _env.canUsePnpm)()) {
79
+ const args = ['info', packageName, 'version'];
80
+
81
+ if (registry) {
82
+ args.push(`--registry=${registry}`);
83
+ }
84
+
85
+ const result = await (0, _execa.default)('pnpm', args);
86
+ spinner.stop();
87
+ return (0, _stripAnsi.stripAnsi)(result.stdout);
88
+ }
89
+
90
+ if (await (0, _env.canUseYarn)()) {
91
+ const args = ['info', packageName, 'version', '--silent'];
92
+
93
+ if (registry) {
94
+ args.push(`--registry=${registry}`);
95
+ }
96
+
97
+ const result = await (0, _execa.default)('yarn', args);
98
+ spinner.stop();
99
+ return (0, _stripAnsi.stripAnsi)(result.stdout);
100
+ }
101
+
102
+ if (await (0, _env.canUseNpm)()) {
103
+ const args = ['view', packageName, 'version'];
104
+
105
+ if (registry) {
106
+ args.push(`--registry=${registry}`);
107
+ }
108
+
109
+ const result = await (0, _execa.default)('npm', args);
110
+ spinner.stop();
111
+ return (0, _stripAnsi.stripAnsi)(result.stdout);
112
+ }
113
+
114
+ spinner.stop();
115
+ throw new Error('not found npm, please install npm before');
116
+ }
117
+
118
+ function getPackageManager(cwd = process.cwd()) {
119
+ let appDirectory = cwd;
120
+
121
+ while (_os.default.homedir() !== appDirectory) {
122
+ if (_utils.fs.existsSync(_path.default.resolve(appDirectory, 'pnpm-lock.yaml'))) {
123
+ return 'pnpm';
124
+ }
125
+
126
+ if (_utils.fs.existsSync(_path.default.resolve(appDirectory, 'yarn.lock'))) {
127
+ return 'yarn';
128
+ }
129
+
130
+ if (_utils.fs.existsSync(_path.default.resolve(appDirectory, 'package-lock.json'))) {
131
+ return 'npm';
132
+ }
133
+
134
+ appDirectory = _path.default.join(appDirectory, '..');
135
+ }
136
+
137
+ return 'npm';
138
+ }
139
+
140
+ function isTsProject(appDir) {
141
+ return _utils.fs.existsSync(_path.default.join(appDir, 'tsconfig.json'));
142
+ }
143
+
144
+ async function getPackageObj(context) {
145
+ const pkgStr = (await context.materials.default.get(`package.json`).value()).content;
146
+ return JSON.parse(pkgStr);
147
+ }
148
+
149
+ function getAllPackages(appDir) {
150
+ const packages = (0, _utils.getMonorepoPackages)(appDir);
151
+ return packages.map(pkg => pkg.name);
152
+ }
153
+
154
+ function validatePackageName(value, packages, options) {
155
+ const {
156
+ isMonorepoSubProject
157
+ } = options;
158
+
159
+ if (isMonorepoSubProject && packages.includes(value)) {
160
+ return {
161
+ success: false,
162
+ error: _locale.i18n.t(_locale.localeKeys.packageName.exit, {
163
+ value
164
+ })
165
+ };
166
+ }
167
+
168
+ return {
169
+ success: true
170
+ };
171
+ }
172
+
173
+ function validatePackagePath(value, projectDir, options) {
174
+ const {
175
+ isMwa,
176
+ isPublic,
177
+ isTest
178
+ } = options || {};
179
+ let dir = 'apps';
180
+
181
+ if (isMwa && isTest) {
182
+ dir = 'examples';
183
+ } else {
184
+ dir = isPublic ? 'packages' : 'features';
185
+ }
186
+
187
+ const packageDir = _path.default.resolve(projectDir || '', dir, value);
188
+
189
+ if (_utils.fs.existsSync(packageDir)) {
190
+ return {
191
+ success: false,
192
+ error: _locale.i18n.t(_locale.localeKeys.pacakgePath.exit, {
193
+ value
194
+ })
195
+ };
196
+ }
197
+
198
+ return {
199
+ success: true
200
+ };
201
+ }
202
+
203
+ function getModuleProjectPath(packagePath, isMonorepoSubProject, isPublic, isLocalPackages) {
204
+ if (isLocalPackages && packagePath) {
205
+ return `${packagePath}/`;
206
+ }
207
+
208
+ if (isMonorepoSubProject && packagePath) {
209
+ return `${isPublic ? 'packages' : 'features'}/${packagePath}/`;
210
+ }
211
+
212
+ return '';
213
+ }
214
+
215
+ function getMWAProjectPath(packagePath, isMonorepoSubProject, isTest = false) {
216
+ if (isMonorepoSubProject && packagePath) {
217
+ return `${isTest ? 'examples' : 'apps'}/${packagePath}/`;
218
+ }
219
+
220
+ return '';
221
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.EN_LOCALE = void 0;
7
+ const EN_LOCALE = {
8
+ packageName: {
9
+ exit: 'package name `{value}` is already exists'
10
+ },
11
+ pacakgePath: {
12
+ exit: 'package path {value} is already exists'
13
+ }
14
+ };
15
+ exports.EN_LOCALE = EN_LOCALE;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.localeKeys = exports.i18n = void 0;
7
+
8
+ var _pluginI18n = require("@modern-js/plugin-i18n");
9
+
10
+ var _zh = require("./zh");
11
+
12
+ var _en = require("./en");
13
+
14
+ const i18n = new _pluginI18n.I18n();
15
+ exports.i18n = i18n;
16
+ const localeKeys = i18n.init('zh', {
17
+ zh: _zh.ZH_LOCALE,
18
+ en: _en.EN_LOCALE
19
+ });
20
+ exports.localeKeys = localeKeys;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ZH_LOCALE = void 0;
7
+ const ZH_LOCALE = {
8
+ packageName: {
9
+ exit: '项目名称 {value} 已存在'
10
+ },
11
+ pacakgePath: {
12
+ exit: '目录 {value} 已存在'
13
+ }
14
+ };
15
+ exports.ZH_LOCALE = ZH_LOCALE;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.canUseNpm = canUseNpm;
7
+ exports.canUsePnpm = canUsePnpm;
8
+ exports.canUseYarn = canUseYarn;
9
+
10
+ var _execa = _interopRequireDefault(require("execa"));
11
+
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+
14
+ async function canUseNpm() {
15
+ try {
16
+ await (0, _execa.default)('npm', ['--version'], {
17
+ env: process.env
18
+ });
19
+ return true;
20
+ } catch (e) {
21
+ return false;
22
+ }
23
+ }
24
+
25
+ async function canUseYarn() {
26
+ try {
27
+ await (0, _execa.default)('yarn', ['--version'], {
28
+ env: process.env
29
+ });
30
+ return true;
31
+ } catch (e) {
32
+ return false;
33
+ }
34
+ }
35
+
36
+ async function canUsePnpm() {
37
+ try {
38
+ await (0, _execa.default)('pnpm', ['--version'], {
39
+ env: process.env
40
+ });
41
+ return true;
42
+ } catch (e) {
43
+ return false;
44
+ }
45
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _env = require("./env");
8
+
9
+ Object.keys(_env).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _env[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _env[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _stripAnsi = require("./strip-ansi");
21
+
22
+ Object.keys(_stripAnsi).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _stripAnsi[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _stripAnsi[key];
29
+ }
30
+ });
31
+ });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.stripAnsi = stripAnsi;
7
+
8
+ function ansiRegex({
9
+ onlyFirst = false
10
+ } = {}) {
11
+ const pattern = ['[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'].join('|');
12
+ return new RegExp(pattern, onlyFirst ? undefined : 'g');
13
+ }
14
+
15
+ function stripAnsi(string) {
16
+ if (typeof string !== 'string') {
17
+ throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
18
+ }
19
+
20
+ return string.replace(ansiRegex(), '');
21
+ }
@@ -0,0 +1,31 @@
1
+ import { GeneratorContext } from '@modern-js/codesmith';
2
+ export * from './utils';
3
+ export { fs } from '@modern-js/utils';
4
+ export { i18n } from './locale';
5
+ export declare function getPackageVersion(packageName: string, registry?: string): Promise<string>;
6
+ export declare function getPackageManager(cwd?: string): "npm" | "yarn" | "pnpm";
7
+ export declare function isTsProject(appDir: string): boolean;
8
+ export declare function getPackageObj(context: GeneratorContext): Promise<any>;
9
+ export declare function getAllPackages(appDir: string): string[];
10
+ export declare function validatePackageName(value: string, packages: string[], options: {
11
+ isMonorepoSubProject: boolean;
12
+ }): {
13
+ success: boolean;
14
+ error: string;
15
+ } | {
16
+ success: boolean;
17
+ error?: undefined;
18
+ };
19
+ export declare function validatePackagePath(value: string, projectDir: string, options?: {
20
+ isMwa?: boolean;
21
+ isPublic?: boolean;
22
+ isTest?: boolean;
23
+ }): {
24
+ success: boolean;
25
+ error: string;
26
+ } | {
27
+ success: boolean;
28
+ error?: undefined;
29
+ };
30
+ export declare function getModuleProjectPath(packagePath: string, isMonorepoSubProject: boolean, isPublic: boolean, isLocalPackages: boolean): string;
31
+ export declare function getMWAProjectPath(packagePath: string, isMonorepoSubProject: boolean, isTest?: boolean): string;
@@ -0,0 +1,8 @@
1
+ export declare const EN_LOCALE: {
2
+ packageName: {
3
+ exit: string;
4
+ };
5
+ pacakgePath: {
6
+ exit: string;
7
+ };
8
+ };
@@ -0,0 +1,18 @@
1
+ import { I18n } from '@modern-js/plugin-i18n';
2
+ declare const i18n: I18n;
3
+ declare const localeKeys: {
4
+ packageName: {
5
+ exit: string;
6
+ };
7
+ pacakgePath: {
8
+ exit: string;
9
+ };
10
+ } | {
11
+ packageName: {
12
+ exit: string;
13
+ };
14
+ pacakgePath: {
15
+ exit: string;
16
+ };
17
+ };
18
+ export { i18n, localeKeys };
@@ -0,0 +1,8 @@
1
+ export declare const ZH_LOCALE: {
2
+ packageName: {
3
+ exit: string;
4
+ };
5
+ pacakgePath: {
6
+ exit: string;
7
+ };
8
+ };
@@ -0,0 +1,3 @@
1
+ export declare function canUseNpm(): Promise<boolean>;
2
+ export declare function canUseYarn(): Promise<boolean>;
3
+ export declare function canUsePnpm(): Promise<boolean>;
@@ -0,0 +1,2 @@
1
+ export * from './env';
2
+ export * from './strip-ansi';
@@ -0,0 +1 @@
1
+ export declare function stripAnsi(string: string): string;
@@ -0,0 +1,8 @@
1
+ /** @type {import('@modern-js/module-tools').UserConfig} */
2
+ module.exports = {
3
+ testing: {
4
+ jest: {
5
+ testTimeout: 30000,
6
+ },
7
+ },
8
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@modern-js/generator-utils",
3
+ "version": "1.0.0-alpha.3",
4
+ "jsnext:source": "./src/index.ts",
5
+ "types": "./dist/types/index.d.ts",
6
+ "main": "./dist/js/node/index.js",
7
+ "module": "./dist/js/treeshaking/index.js",
8
+ "jsnext:modern": "./dist/js/modern/index.js",
9
+ "exports": {
10
+ ".": {
11
+ "node": {
12
+ "import": "./dist/js/modern/index.js",
13
+ "require": "./dist/js/node/index.js"
14
+ },
15
+ "default": "./dist/js/treeshaking/index.js"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "@babel/runtime": "^7",
20
+ "@modern-js/plugin-i18n": "^1.0.0-alpha.3",
21
+ "@modern-js/utils": "^1.0.0-alpha.3",
22
+ "execa": "^5.1.1",
23
+ "ora": "^5.4.1"
24
+ },
25
+ "devDependencies": {
26
+ "@modern-js/codesmith": "^1.0.0-rc.10",
27
+ "@types/jest": "^26",
28
+ "@types/node": "^14",
29
+ "typescript": "^4",
30
+ "@modern-js/module-tools": "^1.0.0-alpha.3",
31
+ "@modern-js/plugin-testing": "^1.0.0-alpha.3"
32
+ },
33
+ "sideEffects": false,
34
+ "modernConfig": {
35
+ "output": {
36
+ "packageMode": "node-js"
37
+ }
38
+ },
39
+ "scripts": {
40
+ "new": "modern new",
41
+ "build": "modern build",
42
+ "test": "modern test --passWithNoTests"
43
+ }
44
+ }
package/src/index.ts ADDED
@@ -0,0 +1,150 @@
1
+ import path from 'path';
2
+ import os from 'os';
3
+ import execa from 'execa';
4
+ import ora from 'ora';
5
+ import { GeneratorContext } from '@modern-js/codesmith';
6
+ import { fs, getMonorepoPackages } from '@modern-js/utils';
7
+ import { canUseNpm, canUsePnpm, canUseYarn } from './utils/env';
8
+ import { stripAnsi } from './utils/strip-ansi';
9
+ import { i18n, localeKeys } from './locale';
10
+
11
+ export * from './utils';
12
+
13
+ export { fs } from '@modern-js/utils';
14
+
15
+ export { i18n } from './locale';
16
+
17
+ // eslint-disable-next-line max-statements
18
+ export async function getPackageVersion(
19
+ packageName: string,
20
+ registry?: string,
21
+ ) {
22
+ const spinner = ora('Loading...').start();
23
+ spinner.color = 'yellow';
24
+ if (await canUsePnpm()) {
25
+ const args = ['info', packageName, 'version'];
26
+ if (registry) {
27
+ args.push(`--registry=${registry}`);
28
+ }
29
+ const result = await execa('pnpm', args);
30
+ spinner.stop();
31
+ return stripAnsi(result.stdout);
32
+ }
33
+ if (await canUseYarn()) {
34
+ const args = ['info', packageName, 'version', '--silent'];
35
+ if (registry) {
36
+ args.push(`--registry=${registry}`);
37
+ }
38
+ const result = await execa('yarn', args);
39
+ spinner.stop();
40
+ return stripAnsi(result.stdout);
41
+ }
42
+ if (await canUseNpm()) {
43
+ const args = ['view', packageName, 'version'];
44
+ if (registry) {
45
+ args.push(`--registry=${registry}`);
46
+ }
47
+ const result = await execa('npm', args);
48
+ spinner.stop();
49
+ return stripAnsi(result.stdout);
50
+ }
51
+ spinner.stop();
52
+ throw new Error('not found npm, please install npm before');
53
+ }
54
+
55
+ export function getPackageManager(cwd: string = process.cwd()) {
56
+ let appDirectory = cwd;
57
+ while (os.homedir() !== appDirectory) {
58
+ if (fs.existsSync(path.resolve(appDirectory, 'pnpm-lock.yaml'))) {
59
+ return 'pnpm';
60
+ }
61
+ if (fs.existsSync(path.resolve(appDirectory, 'yarn.lock'))) {
62
+ return 'yarn';
63
+ }
64
+ if (fs.existsSync(path.resolve(appDirectory, 'package-lock.json'))) {
65
+ return 'npm';
66
+ }
67
+ appDirectory = path.join(appDirectory, '..');
68
+ }
69
+ return 'npm';
70
+ }
71
+
72
+ export function isTsProject(appDir: string) {
73
+ return fs.existsSync(path.join(appDir, 'tsconfig.json'));
74
+ }
75
+
76
+ export async function getPackageObj(context: GeneratorContext) {
77
+ const pkgStr = (await context.materials.default.get(`package.json`).value())
78
+ .content;
79
+
80
+ return JSON.parse(pkgStr as string);
81
+ }
82
+
83
+ export function getAllPackages(appDir: string) {
84
+ const packages = getMonorepoPackages(appDir);
85
+ return packages.map(pkg => pkg.name);
86
+ }
87
+
88
+ export function validatePackageName(
89
+ value: string,
90
+ packages: string[],
91
+ options: { isMonorepoSubProject: boolean },
92
+ ) {
93
+ const { isMonorepoSubProject } = options;
94
+ if (isMonorepoSubProject && packages.includes(value)) {
95
+ return {
96
+ success: false,
97
+ error: i18n.t(localeKeys.packageName.exit, { value }),
98
+ };
99
+ }
100
+ return { success: true };
101
+ }
102
+
103
+ export function validatePackagePath(
104
+ value: string,
105
+ projectDir: string,
106
+ options?: { isMwa?: boolean; isPublic?: boolean; isTest?: boolean },
107
+ ) {
108
+ const { isMwa, isPublic, isTest } = options || {};
109
+ let dir = 'apps';
110
+ if (isMwa && isTest) {
111
+ dir = 'examples';
112
+ } else {
113
+ dir = isPublic ? 'packages' : 'features';
114
+ }
115
+ const packageDir = path.resolve(projectDir || '', dir, value);
116
+ if (fs.existsSync(packageDir)) {
117
+ return {
118
+ success: false,
119
+ error: i18n.t(localeKeys.pacakgePath.exit, { value }),
120
+ };
121
+ }
122
+ return { success: true };
123
+ }
124
+
125
+ export function getModuleProjectPath(
126
+ packagePath: string,
127
+ isMonorepoSubProject: boolean,
128
+ isPublic: boolean,
129
+ isLocalPackages: boolean,
130
+ ) {
131
+ if (isLocalPackages && packagePath) {
132
+ return `${packagePath}/`;
133
+ }
134
+ if (isMonorepoSubProject && packagePath) {
135
+ return `${isPublic ? 'packages' : 'features'}/${packagePath}/`;
136
+ }
137
+
138
+ return '';
139
+ }
140
+
141
+ export function getMWAProjectPath(
142
+ packagePath: string,
143
+ isMonorepoSubProject: boolean,
144
+ isTest = false,
145
+ ) {
146
+ if (isMonorepoSubProject && packagePath) {
147
+ return `${isTest ? 'examples' : 'apps'}/${packagePath}/`;
148
+ }
149
+ return '';
150
+ }
@@ -0,0 +1,4 @@
1
+ export const EN_LOCALE = {
2
+ packageName: { exit: 'package name `{value}` is already exists' },
3
+ pacakgePath: { exit: 'package path {value} is already exists' },
4
+ };
@@ -0,0 +1,9 @@
1
+ import { I18n } from '@modern-js/plugin-i18n';
2
+ import { ZH_LOCALE } from './zh';
3
+ import { EN_LOCALE } from './en';
4
+
5
+ const i18n = new I18n();
6
+
7
+ const localeKeys = i18n.init('zh', { zh: ZH_LOCALE, en: EN_LOCALE });
8
+
9
+ export { i18n, localeKeys };
@@ -0,0 +1,4 @@
1
+ export const ZH_LOCALE = {
2
+ packageName: { exit: '项目名称 {value} 已存在' },
3
+ pacakgePath: { exit: '目录 {value} 已存在' },
4
+ };
@@ -0,0 +1,28 @@
1
+ import execa from 'execa';
2
+
3
+ export async function canUseNpm() {
4
+ try {
5
+ await execa('npm', ['--version'], { env: process.env });
6
+ return true;
7
+ } catch (e) {
8
+ return false;
9
+ }
10
+ }
11
+
12
+ export async function canUseYarn() {
13
+ try {
14
+ await execa('yarn', ['--version'], { env: process.env });
15
+ return true;
16
+ } catch (e) {
17
+ return false;
18
+ }
19
+ }
20
+
21
+ export async function canUsePnpm() {
22
+ try {
23
+ await execa('pnpm', ['--version'], { env: process.env });
24
+ return true;
25
+ } catch (e) {
26
+ return false;
27
+ }
28
+ }
@@ -0,0 +1,2 @@
1
+ export * from './env';
2
+ export * from './strip-ansi';
@@ -0,0 +1,16 @@
1
+ function ansiRegex({ onlyFirst = false } = {}) {
2
+ const pattern = [
3
+ '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
4
+ '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))',
5
+ ].join('|');
6
+
7
+ return new RegExp(pattern, onlyFirst ? undefined : 'g');
8
+ }
9
+
10
+ export function stripAnsi(string: string) {
11
+ if (typeof string !== 'string') {
12
+ throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
13
+ }
14
+
15
+ return string.replace(ansiRegex(), '');
16
+ }
package/tests/index.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { getPackageVersion, canUseNpm, canUsePnpm, canUseYarn } from '..';
2
+
3
+ describe('test generator utils', () => {
4
+ test('test getPackageVersion', async () => {
5
+ const version = await getPackageVersion('lodash');
6
+ expect(typeof version === 'string').toBe(true);
7
+ });
8
+ test('test canUseNpm', async () => {
9
+ const npmAbility = await canUseNpm();
10
+ expect(typeof npmAbility === 'boolean').toBe(true);
11
+ });
12
+ test('test canUsePnpm', async () => {
13
+ const pnpmAbility = await canUsePnpm();
14
+ expect(typeof pnpmAbility === 'boolean').toBe(true);
15
+ });
16
+ test('test canUseYarn', async () => {
17
+ const yarnAbility = await canUseYarn();
18
+ expect(typeof yarnAbility === 'boolean').toBe(true);
19
+ });
20
+ });
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "@modern-js/tsconfig/base",
3
+ "compilerOptions": {
4
+ "declaration": false,
5
+ "jsx": "preserve",
6
+ "baseUrl": "./",
7
+ "paths": {
8
+ "@/*": ["../src/*"]
9
+ }
10
+ }
11
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "@modern-js/tsconfig/base",
3
+ "compilerOptions": {
4
+ "declaration": false,
5
+ "jsx": "preserve",
6
+ "baseUrl": "./",
7
+ "paths": {
8
+ "@/*": ["./src/*"]
9
+ }
10
+ },
11
+ "include": ["src"]
12
+ }