2018-04-08 23:03:39 +00:00
|
|
|
import * as q from 'smartq';
|
|
|
|
import { ProjectinfoNpm } from 'projectinfo';
|
2016-10-21 15:48:52 +00:00
|
|
|
|
2017-06-21 18:54:28 +00:00
|
|
|
// interfaces
|
2018-04-08 23:03:39 +00:00
|
|
|
import { INpmtsConfig } from '../npmts.config';
|
2017-06-21 18:54:28 +00:00
|
|
|
|
2018-04-08 23:03:39 +00:00
|
|
|
import * as paths from '../npmts.paths';
|
2016-10-02 18:35:13 +00:00
|
|
|
|
2018-04-08 23:03:39 +00:00
|
|
|
import * as plugins from './mod.plugins';
|
2016-09-02 12:10:09 +00:00
|
|
|
|
2018-04-08 23:03:39 +00:00
|
|
|
export let projectInfo: ProjectinfoNpm;
|
2016-09-02 12:10:09 +00:00
|
|
|
|
2017-06-21 18:54:28 +00:00
|
|
|
let checkProjectTypings = (configArg: INpmtsConfig) => {
|
2018-04-08 23:03:39 +00:00
|
|
|
let done = q.defer<INpmtsConfig>();
|
|
|
|
plugins.beautylog.ora.text('Check Module: Check Project Typings...');
|
|
|
|
projectInfo = new ProjectinfoNpm(paths.cwd);
|
2017-05-04 22:01:21 +00:00
|
|
|
if (typeof projectInfo.packageJson.typings === 'undefined') {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.error(`please add typings field to package.json`);
|
|
|
|
process.exit(1);
|
2017-11-28 16:33:55 +00:00
|
|
|
}
|
2018-04-08 23:03:39 +00:00
|
|
|
done.resolve(configArg);
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-07-28 01:10:51 +00:00
|
|
|
|
2016-07-28 01:45:14 +00:00
|
|
|
const depcheckOptions = {
|
2017-05-04 22:01:21 +00:00
|
|
|
ignoreBinPackage: false, // ignore the packages with bin entry
|
2018-04-08 23:03:39 +00:00
|
|
|
parsers: {
|
|
|
|
// the target parsers
|
2017-05-04 22:01:21 +00:00
|
|
|
'*.ts': plugins.depcheck.parser.typescript
|
|
|
|
},
|
2018-04-08 23:03:39 +00:00
|
|
|
detectors: [
|
|
|
|
// the target detectors
|
2017-05-04 22:01:21 +00:00
|
|
|
plugins.depcheck.detector.requireCallExpression,
|
|
|
|
plugins.depcheck.detector.importDeclaration
|
|
|
|
],
|
2018-04-08 23:03:39 +00:00
|
|
|
specials: [
|
|
|
|
// the target special parsers
|
2017-05-04 22:01:21 +00:00
|
|
|
plugins.depcheck.special.eslint,
|
|
|
|
plugins.depcheck.special.webpack
|
|
|
|
]
|
2018-04-08 23:03:39 +00:00
|
|
|
};
|
2016-07-28 01:45:14 +00:00
|
|
|
|
2017-06-21 18:54:28 +00:00
|
|
|
let checkDependencies = (configArg: INpmtsConfig) => {
|
2018-04-08 23:03:39 +00:00
|
|
|
let done = q.defer<INpmtsConfig>();
|
|
|
|
plugins.beautylog.ora.text('Check Module: Check Dependencies...');
|
2017-05-04 22:01:21 +00:00
|
|
|
let depcheckOptionsMerged = plugins.lodash.merge(depcheckOptions, {
|
2018-04-08 23:03:39 +00:00
|
|
|
ignoreDirs: [
|
|
|
|
// folder with these names will be ignored
|
2017-05-04 22:01:21 +00:00
|
|
|
'test',
|
|
|
|
'dist',
|
|
|
|
'bower_components'
|
|
|
|
],
|
2018-04-08 23:03:39 +00:00
|
|
|
ignoreMatches: [
|
|
|
|
// ignore dependencies that matches these globs
|
2017-05-04 22:01:21 +00:00
|
|
|
'@types/*',
|
|
|
|
'babel-preset-*'
|
|
|
|
]
|
2018-04-08 23:03:39 +00:00
|
|
|
});
|
|
|
|
plugins.depcheck(paths.cwd, depcheckOptionsMerged, unused => {
|
2017-05-04 22:01:21 +00:00
|
|
|
for (let item of unused.dependencies) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.warn(`Watch out: unused dependency "${item}"`);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
for (let item in unused.missing) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.error(`missing dependency "${item}" in package.json`);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
if (unused.missing.length > 0) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.info('exiting due to missing dependencies in package.json');
|
|
|
|
process.exit(1);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
for (let item in unused.invalidFiles) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.warn(`Watch out: could not parse file ${item}`);
|
2017-11-28 16:33:55 +00:00
|
|
|
}
|
2017-05-04 22:01:21 +00:00
|
|
|
for (let item in unused.invalidDirs) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.warn(`Watch out: could not parse directory ${item}`);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
2018-04-08 23:03:39 +00:00
|
|
|
done.resolve(configArg);
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-07-28 01:45:14 +00:00
|
|
|
|
2017-06-21 18:54:28 +00:00
|
|
|
let checkDevDependencies = (configArg: INpmtsConfig) => {
|
2018-04-08 23:03:39 +00:00
|
|
|
let done = q.defer<INpmtsConfig>();
|
|
|
|
plugins.beautylog.ora.text('Check Module: Check devDependencies...');
|
2017-05-04 22:01:21 +00:00
|
|
|
let depcheckOptionsMerged = plugins.lodash.merge(depcheckOptions, {
|
2018-04-08 23:03:39 +00:00
|
|
|
ignoreDirs: [
|
|
|
|
// folder with these names will be ignored
|
2017-05-04 22:01:21 +00:00
|
|
|
'ts',
|
|
|
|
'dist',
|
|
|
|
'bower_components'
|
|
|
|
],
|
2018-04-08 23:03:39 +00:00
|
|
|
ignoreMatches: [
|
|
|
|
// ignore dependencies that matches these globs
|
2017-05-04 22:01:21 +00:00
|
|
|
'@types/*',
|
|
|
|
'babel-preset-*'
|
|
|
|
]
|
2018-04-08 23:03:39 +00:00
|
|
|
});
|
|
|
|
plugins.depcheck(paths.cwd, depcheckOptionsMerged, unused => {
|
2017-05-04 22:01:21 +00:00
|
|
|
for (let item of unused.devDependencies) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.log(`unused devDependency ${item}`);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
for (let item in unused.missing) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.error(`missing devDependency ${item}`);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
if (unused.missing.length > 0) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.info('exiting due to missing dependencies in package.json');
|
|
|
|
process.exit(1);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
for (let item in unused.invalidFiles) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.warn(`Watch out: could not parse file ${item}`);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
|
|
|
for (let item in unused.invalidDirs) {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.warn(`Watch out: could not parse directory ${item}`);
|
2017-05-04 22:01:21 +00:00
|
|
|
}
|
2018-04-08 23:03:39 +00:00
|
|
|
done.resolve(configArg);
|
|
|
|
});
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-07-28 01:10:51 +00:00
|
|
|
|
2017-06-21 18:54:28 +00:00
|
|
|
let checkNodeVersion = (configArg: INpmtsConfig) => {
|
2018-04-08 23:03:39 +00:00
|
|
|
let done = q.defer<INpmtsConfig>();
|
|
|
|
plugins.beautylog.ora.text('checking node version');
|
|
|
|
done.resolve(configArg);
|
|
|
|
return done.promise;
|
|
|
|
};
|
2016-07-28 01:10:51 +00:00
|
|
|
|
2017-06-21 18:54:28 +00:00
|
|
|
export let run = async (configArg: INpmtsConfig) => {
|
2018-04-08 23:03:39 +00:00
|
|
|
plugins.beautylog.ora.text('Check Module: ...');
|
2017-06-15 23:42:48 +00:00
|
|
|
|
|
|
|
if (configArg.checkDependencies) {
|
2018-04-08 23:03:39 +00:00
|
|
|
configArg = await checkProjectTypings(configArg);
|
|
|
|
configArg = await checkDependencies(configArg);
|
|
|
|
configArg = await checkDevDependencies(configArg);
|
|
|
|
configArg = await checkNodeVersion(configArg);
|
|
|
|
return configArg;
|
2017-06-15 23:42:48 +00:00
|
|
|
} else {
|
2018-04-08 23:03:39 +00:00
|
|
|
configArg = await checkProjectTypings(configArg);
|
|
|
|
return configArg;
|
2017-06-15 23:42:48 +00:00
|
|
|
}
|
2018-04-08 23:03:39 +00:00
|
|
|
};
|