-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (89 loc) · 2.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* eslint-disable no-console */
'use strict';
const ora = require( 'ora' );
const path = require( 'path' );
const readPkgUp = require( 'read-pkg-up' );
const writePkg = require( 'write-pkg' );
const execa = require( 'execa' );
const shell = require( 'shelljs' );
const hasYarn = require( 'has-yarn' );
const DEFAULT_TEST_SCRIPT = 'echo "Error: no test specified" && exit 1';
const buildTestScript = ( test ) => {
if ( test && test !== DEFAULT_TEST_SCRIPT ) {
// Don't add if it's already there
if ( ! /^npm run -s test:axe( |$)/.test( test ) ) {
return `npm run -s test:axe && ${ test }`;
}
return test;
}
return 'npm run -s test:axe';
};
const copyFiles = ( cwd ) => {
const sourceDir = `${ cwd }/node_modules/wp-theme-auditor`;
const destinationDir = cwd;
shell.cp( '-u', `${ sourceDir }/babel.config.js`, destinationDir );
shell.cp( '-u', `${ sourceDir }/jest.config.js`, destinationDir );
shell.mkdir( '-p', `${ destinationDir }/test/support` );
shell.cp( `${ sourceDir }/test/*.js`, `${ destinationDir }/test/` );
shell.cp( `${ sourceDir }/test/support/*`, `${ destinationDir }/test/support/` );
return destinationDir;
};
module.exports = async ( options = {} ) => {
const packageResult = readPkgUp.sync( {
cwd: options.cwd,
normalize: false,
} ) || {};
const packageJson = packageResult.packageJson || {};
const packagePath = packageResult.path || path.resolve( options.cwd || '', 'package.json' );
const packageCwd = path.dirname( packagePath );
const scriptsSpinner = ora( 'Adding test scripts...' );
const configSpinner = ora( 'Installing test files...' );
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts[ 'test:axe' ] = 'wp-scripts test-e2e';
packageJson.scripts[ 'create-test-cases' ] = 'create-test-cases';
packageJson.scripts.test = buildTestScript( packageJson.scripts.test );
if ( ! options.quiet ) {
scriptsSpinner.start();
}
writePkg.sync( packagePath, packageJson );
if ( ! options.quiet ) {
scriptsSpinner.succeed();
}
const post = () => {
if ( ! options.quiet ) {
configSpinner.start();
}
copyFiles( packageCwd );
if ( ! options.quiet ) {
configSpinner.succeed();
}
};
if ( options.skipInstall ) {
return;
}
const depsSpinner = ora( 'Adding wp-theme-auditor to dependencies...' );
if ( ! options.quiet ) {
depsSpinner.start();
}
if ( hasYarn( packageCwd ) ) {
try {
await execa( 'yarn', [ 'add', '--dev', '--ignore-workspace-root-check', 'wp-theme-auditor' ], { cwd: packageCwd } );
if ( ! options.quiet ) {
depsSpinner.succeed();
}
post();
} catch ( error ) {
if ( error.code === 'ENOENT' ) {
console.error( 'This project uses Yarn but you don\'t seem to have Yarn installed.\nRun `npm install --global yarn` to install it.' );
return;
}
throw error;
}
return;
}
await execa( 'npm', [ 'install', '--save-dev', 'wp-theme-auditor' ], { cwd: packageCwd } );
if ( ! options.quiet ) {
depsSpinner.succeed();
}
post();
};