1 |
|
2 | import fs from 'fs';
|
3 | import path from 'path';
|
4 | import osenv from 'osenv';
|
5 | import spawn from 'cross-spawn';
|
6 | import packageJson from '../../shared/packageJson';
|
7 | import {
|
8 | FEFLOW_ROOT,
|
9 | UNIVERSAL_MODULES,
|
10 | LATEST_VERSION
|
11 | } from '../../shared/constant';
|
12 | import { getCurrentTag } from '../universal-pkg/repository/git';
|
13 | import loggerInstance from '../logger';
|
14 | import versionImpl from '../universal-pkg/dep/version';
|
15 |
|
16 | interface VersionObj {
|
17 | name: string;
|
18 | localVersion: any;
|
19 | latestVersion: any;
|
20 | repoPath: string;
|
21 | installVersion: any;
|
22 | }
|
23 |
|
24 | const { debug, silent } = process.env;
|
25 | const root = path.join(osenv.home(), FEFLOW_ROOT);
|
26 | const rootPkg = path.join(root, 'package.json');
|
27 | const universalModulesPath = path.join(root, UNIVERSAL_MODULES);
|
28 |
|
29 | const logger = loggerInstance({
|
30 | debug: Boolean(debug),
|
31 | silent: Boolean(silent)
|
32 | });
|
33 |
|
34 | export const getInstalledPlugins = () => {
|
35 | let plugins: any = [];
|
36 | const exist = fs.existsSync(rootPkg);
|
37 | const pluginDir = path.join(root, 'node_modules');
|
38 |
|
39 | if (!exist) {
|
40 | plugins = [];
|
41 | } else {
|
42 | const content: any = fs.readFileSync(rootPkg);
|
43 |
|
44 | let json: any;
|
45 |
|
46 | try {
|
47 | json = JSON.parse(content);
|
48 | const deps = json.dependencies || json.devDependencies || {};
|
49 |
|
50 | plugins = Object.keys(deps);
|
51 | } catch (ex) {
|
52 | plugins = [];
|
53 | }
|
54 | }
|
55 | return plugins.filter((name: any) => {
|
56 | if (
|
57 | !/^feflow-plugin-|^@[^/]+\/feflow-plugin-|generator-|^@[^/]+\/generator-/.test(
|
58 | name
|
59 | )
|
60 | ) {
|
61 | return false;
|
62 | }
|
63 | const pathFn = path.join(pluginDir, name);
|
64 | return fs.existsSync(pathFn);
|
65 | });
|
66 | };
|
67 |
|
68 | export const getNpmRegistryUrl = (packageManager: string) => {
|
69 | return spawn
|
70 | .sync(packageManager, ['config', 'get', 'registry'], { windowsHide: true })
|
71 | .stdout.toString()
|
72 | .replace(/\n/, '')
|
73 | .replace(/\/$/, '');
|
74 | };
|
75 |
|
76 | export const getLatestVersion = async (
|
77 | name: string,
|
78 | packageManager: string
|
79 | ) => {
|
80 | const registryUrl = getNpmRegistryUrl(packageManager);
|
81 | return await packageJson(name, registryUrl).catch(() => {
|
82 | logger.warn(
|
83 | `Network error, can't reach ${registryUrl}, CLI give up verison check.`
|
84 | );
|
85 | });
|
86 | };
|
87 |
|
88 | export const updatePluginsVersion = (packagePath: string, plugins: any) => {
|
89 | const obj = require(packagePath);
|
90 |
|
91 | plugins.map((plugin: any) => {
|
92 | obj.dependencies[plugin.name] = plugin.latestVersion;
|
93 | });
|
94 |
|
95 | fs.writeFileSync(packagePath, JSON.stringify(obj, null, 4));
|
96 | };
|
97 |
|
98 | export const getUniversalPluginVersion = (pkgInfo: any, universalPkg: any) => {
|
99 | return new Promise<VersionObj>(async resolve => {
|
100 | const repoPath = path.join(
|
101 | universalModulesPath,
|
102 | `${pkgInfo.repoName}@${pkgInfo.installVersion}`
|
103 | );
|
104 | if (pkgInfo.installVersion === LATEST_VERSION) {
|
105 | if (universalPkg.isInstalled(pkgInfo.repoName, LATEST_VERSION)) {
|
106 | const currentVersion = await getCurrentTag(repoPath) || '';
|
107 | if (versionImpl.gt(pkgInfo.checkoutTag, currentVersion)) {
|
108 | resolve({
|
109 | name: pkgInfo.repoName,
|
110 | localVersion: currentVersion,
|
111 | latestVersion: pkgInfo.checkoutTag,
|
112 | repoPath,
|
113 | installVersion: pkgInfo.installVersion
|
114 | });
|
115 | }
|
116 | }
|
117 | }
|
118 | resolve({
|
119 | name: pkgInfo.repoName,
|
120 | localVersion: '',
|
121 | latestVersion: '',
|
122 | repoPath,
|
123 | installVersion: pkgInfo.installVersion
|
124 | });
|
125 | });
|
126 | };
|
127 |
|
128 | export const promisify = (asyncFun: Function, ...args: any) => {
|
129 | return () => {
|
130 | return new Promise<undefined>(async resolve => {
|
131 | await asyncFun(...args);
|
132 | resolve();
|
133 | });
|
134 | };
|
135 | };
|