|
@@ -4,15 +4,19 @@ import execa = require('execa');
|
|
|
import path = require('path');
|
|
import path = require('path');
|
|
|
import fs = require('fs');
|
|
import fs = require('fs');
|
|
|
import glob = require('glob');
|
|
import glob = require('glob');
|
|
|
|
|
+import util = require('util');
|
|
|
|
|
+import { Linter, Configuration, RuleFailure } from 'tslint';
|
|
|
|
|
+import * as prettier from 'prettier';
|
|
|
|
|
|
|
|
import { useSpinner } from '../utils/useSpinner';
|
|
import { useSpinner } from '../utils/useSpinner';
|
|
|
-import { Linter, Configuration, RuleFailure } from 'tslint';
|
|
|
|
|
import { testPlugin } from './plugin/tests';
|
|
import { testPlugin } from './plugin/tests';
|
|
|
import { bundlePlugin as bundleFn, PluginBundleOptions } from './plugin/bundle';
|
|
import { bundlePlugin as bundleFn, PluginBundleOptions } from './plugin/bundle';
|
|
|
|
|
+
|
|
|
interface PrecommitOptions {}
|
|
interface PrecommitOptions {}
|
|
|
|
|
|
|
|
export const bundlePlugin = useSpinner<PluginBundleOptions>('Compiling...', async options => await bundleFn(options));
|
|
export const bundlePlugin = useSpinner<PluginBundleOptions>('Compiling...', async options => await bundleFn(options));
|
|
|
|
|
|
|
|
|
|
+const readFileAsync = util.promisify(fs.readFile);
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
export const clean = useSpinner<void>('Cleaning', async () => await execa('rimraf', [`${process.cwd()}/dist`]));
|
|
export const clean = useSpinner<void>('Cleaning', async () => await execa('rimraf', [`${process.cwd()}/dist`]));
|
|
|
|
|
|
|
@@ -36,20 +40,67 @@ const typecheckPlugin = useSpinner<void>('Typechecking', async () => {
|
|
|
await execa('tsc', ['--noEmit']);
|
|
await execa('tsc', ['--noEmit']);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+const getTypescriptSources = () => {
|
|
|
|
|
+ const globPattern = path.resolve(process.cwd(), 'src/**/*.+(ts|tsx)');
|
|
|
|
|
+ return glob.sync(globPattern);
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const getStylesSources = () => {
|
|
|
|
|
+ const globPattern = path.resolve(process.cwd(), 'src/**/*.+(scss|css)');
|
|
|
|
|
+ return glob.sync(globPattern);
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const prettierCheckPlugin = useSpinner<void>('Prettier check', async () => {
|
|
|
|
|
+ const prettierConfig = require(path.resolve(__dirname, '../../config/prettier.plugin.config.json'));
|
|
|
|
|
+ const sources = [...getStylesSources(), ...getTypescriptSources()];
|
|
|
|
|
+
|
|
|
|
|
+ const promises = sources.map((s, i) => {
|
|
|
|
|
+ return new Promise<{ path: string; failed: boolean }>((resolve, reject) => {
|
|
|
|
|
+ fs.readFile(s, (err, data) => {
|
|
|
|
|
+ let failed = false;
|
|
|
|
|
+ if (err) {
|
|
|
|
|
+ throw new Error(err.message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (
|
|
|
|
|
+ !prettier.check(data.toString(), {
|
|
|
|
|
+ ...prettierConfig,
|
|
|
|
|
+ filepath: s,
|
|
|
|
|
+ })
|
|
|
|
|
+ ) {
|
|
|
|
|
+ failed = true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ resolve({
|
|
|
|
|
+ path: s,
|
|
|
|
|
+ failed,
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ const results = await Promise.all(promises);
|
|
|
|
|
+ const failures = results.filter(r => r.failed);
|
|
|
|
|
+ if (failures.length) {
|
|
|
|
|
+ console.log('\nFix Prettier issues in following files:');
|
|
|
|
|
+ failures.forEach(f => console.log(f.path));
|
|
|
|
|
+ throw new Error('Prettier failed');
|
|
|
|
|
+ }
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
const lintPlugin = useSpinner<void>('Linting', async () => {
|
|
const lintPlugin = useSpinner<void>('Linting', async () => {
|
|
|
let tsLintConfigPath = path.resolve(process.cwd(), 'tslint.json');
|
|
let tsLintConfigPath = path.resolve(process.cwd(), 'tslint.json');
|
|
|
if (!fs.existsSync(tsLintConfigPath)) {
|
|
if (!fs.existsSync(tsLintConfigPath)) {
|
|
|
tsLintConfigPath = path.resolve(__dirname, '../../config/tslint.plugin.json');
|
|
tsLintConfigPath = path.resolve(__dirname, '../../config/tslint.plugin.json');
|
|
|
}
|
|
}
|
|
|
- const globPattern = path.resolve(process.cwd(), 'src/**/*.+(ts|tsx)');
|
|
|
|
|
- const sourcesToLint = glob.sync(globPattern);
|
|
|
|
|
const options = {
|
|
const options = {
|
|
|
fix: true, // or fail
|
|
fix: true, // or fail
|
|
|
formatter: 'json',
|
|
formatter: 'json',
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const configuration = Configuration.findConfiguration(tsLintConfigPath).results;
|
|
const configuration = Configuration.findConfiguration(tsLintConfigPath).results;
|
|
|
|
|
+ const sourcesToLint = getTypescriptSources();
|
|
|
|
|
|
|
|
const lintResults = sourcesToLint
|
|
const lintResults = sourcesToLint
|
|
|
.map(fileName => {
|
|
.map(fileName => {
|
|
@@ -81,9 +132,9 @@ const lintPlugin = useSpinner<void>('Linting', async () => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const pluginBuildRunner: TaskRunner<PrecommitOptions> = async () => {
|
|
const pluginBuildRunner: TaskRunner<PrecommitOptions> = async () => {
|
|
|
- // console.log('asasas')
|
|
|
|
|
await clean();
|
|
await clean();
|
|
|
await prepare();
|
|
await prepare();
|
|
|
|
|
+ await prettierCheckPlugin();
|
|
|
// @ts-ignore
|
|
// @ts-ignore
|
|
|
await lintPlugin();
|
|
await lintPlugin();
|
|
|
await testPlugin({ updateSnapshot: false, coverage: false });
|
|
await testPlugin({ updateSnapshot: false, coverage: false });
|