Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
feat: add remax version check in project (#325)
Browse files Browse the repository at this point in the history
* fix: add remax version check in project

* fix: add symbol type

* fix: add symbol type

* fix: change remax path to node_modules

* fix: change path.join to require.resolve

* fix: semver.lt => semver.neq
  • Loading branch information
QC-L authored and yesmeck committed Nov 8, 2019
1 parent 6cf6dea commit 6ab19ce
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/remax-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-node-resolve": "^5.0.3",
"rollup-plugin-progress": "^1.1.1",
"semver": "^6.3.0",
"slash2": "^2.0.0",
"yargs": "^13.2.4"
},
Expand Down
70 changes: 70 additions & 0 deletions packages/remax-cli/src/checkVersions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import path from 'path';
import fs from 'fs';
import semver from 'semver';
import chalk from 'chalk';

const generatorStar = (count: number, symbol = '*') => {
let starStr = '';
for (let i = 0; i < count; i++) {
starStr += symbol;
}
return starStr;
};

const generatorPlace = (
origin: string,
replace: string,
longLength: number
): string => {
const index = Math.round(origin.length / 2) - Math.round(longLength / 2) - 1;
const length = replace.length;
const originArray = origin.split('');
const replaceArray = replace.split('');
for (let i = 0; i < length; i++) {
originArray[i + index] = replaceArray[i];
}
return originArray.join('');
};

export const checkRemaxVersion = () => {
try {
const cliPackagePath: string = require.resolve('../package.json');
const remaxPackagePath: string = path.join(
process.cwd(),
'node_modules',
'remax',
'package.json'
);
if (fs.existsSync(remaxPackagePath)) {
const remaxPkgConfig = require(remaxPackagePath);
const cliPkgConfig = require(cliPackagePath);
const remaxVersion = remaxPkgConfig.version;

if (semver.neq(remaxVersion, cliPkgConfig.version)) {
const placeholder =
'* *';
const origin =
'* *';
const remax = 'remax: ' + chalk.red(remaxVersion);
const remaxCLI = 'remax-cli: ' + chalk.green(cliPkgConfig.version);
const longLength =
remax.length > remaxCLI.length ? remax.length : remaxCLI.length;

const warning = chalk.yellow(`remax-cli 版本与 remax 版本不匹配`);
const upgrade = chalk.yellow(`请升级 remax 版本`);

console.log(generatorStar(55));
console.log(placeholder);
console.log('*', ` ${warning} `, '*');
console.log('*', ` ${upgrade} `, '*');
console.log(placeholder);
console.log(generatorPlace(origin, remax, longLength));
console.log(generatorPlace(origin, remaxCLI, longLength));
console.log(placeholder);
console.log(generatorStar(55));
}
}
} catch (err) {
return false;
}
};
2 changes: 2 additions & 0 deletions packages/remax-cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import cli from 'yargs';
import build from './build';
import { checkRemaxVersion } from './checkVersions';

export function run(args: any, context?: any) {
checkRemaxVersion();
cli
.scriptName('remax-cli')
.usage('Usage: $0 <command> [options]')
Expand Down

1 comment on commit 6ab19ce

@vercel
Copy link

@vercel vercel bot commented on 6ab19ce Nov 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.