1 | const path = require('path')
|
2 | const fs = require('fs')
|
3 | const debug = require('debug')('spdt')
|
4 |
|
5 | const STORYBOOK_PORT = process.env.STORYBOOK_PORT || 9009
|
6 |
|
7 | const SPDT_DIR = './.spdt'
|
8 |
|
9 | const DEFAULT_CONFIG = {
|
10 | pathToSrc: './src',
|
11 | pathToStories: SPDT_DIR,
|
12 | pathToTestIndex: SPDT_DIR,
|
13 | testIndexName: 'test-index.generated.ts',
|
14 | }
|
15 |
|
16 | const PACKAGE_SCRIPTS = {
|
17 | 'spdt:generate-story-index': './node_modules/.bin/spdt:generate-story-index',
|
18 | 'spdt:generate-test-index': './node_modules/.bin/spdt:generate-test-index',
|
19 | 'spdt:generate-tests': './node_modules/.bin/spdt:generate-tests',
|
20 | 'spdt:test': `jest --detectOpenHandles --config ${SPDT_DIR}/jest.spdt.config.js --runInBand`,
|
21 | 'spdt:test:chrome': `HEADLESS=false jest --detectOpenHandles --config ${SPDT_DIR}/jest.spdt.config.js`,
|
22 | 'spdt:test:chrome:slow': `SLOWMO=1000 HEADLESS=false jest --detectOpenHandles --config ${SPDT_DIR}/jest.spdt.config.js`,
|
23 | 'spdt:storybook': `start-storybook -p ${STORYBOOK_PORT} -c ${SPDT_DIR}`,
|
24 | 'spdt:storybook:ci': `start-storybook --ci --quiet -p ${STORYBOOK_PORT} -c ${SPDT_DIR}`,
|
25 | spdt:
|
26 | 'npm run spdt:generate-story-index && npm run spdt:generate-test-index && npm run spdt:generate-tests && npm run spdt:storybook',
|
27 | 'spdt:ci':
|
28 | 'npm run spdt:generate-story-index && npm run spdt:generate-test-index && npm run spdt:generate-tests && npm run spdt:storybook:ci',
|
29 | ci: 'start-server-and-test spdt:ci 9009 spdt:test',
|
30 | }
|
31 |
|
32 | const PACKAGE_JSON_CONFIG_KEY = 'spdt'
|
33 |
|
34 | function findPackageJson(startDir) {
|
35 | let dir = path.resolve(startDir || process.cwd())
|
36 |
|
37 | do {
|
38 | const pkgFile = path.join(dir, 'package.json')
|
39 |
|
40 | if (!fs.existsSync(pkgFile) || !fs.statSync(pkgFile).isFile()) {
|
41 | dir = path.join(dir, '..')
|
42 | } else {
|
43 | return pkgFile
|
44 | }
|
45 | } while (dir !== path.resolve(dir, '..'))
|
46 | return null
|
47 | }
|
48 |
|
49 | function getConfig(configPath) {
|
50 |
|
51 | const packagePath = configPath === null
|
52 | ? configPath
|
53 | : configPath || findPackageJson()
|
54 | try {
|
55 | const packageJsonConfig = (packagePath && require(packagePath)) || {}
|
56 | const projectRoot = packagePath && {
|
57 | projectRoot: path.dirname(packagePath),
|
58 | }
|
59 | return Object.assign(
|
60 | {},
|
61 | DEFAULT_CONFIG,
|
62 | packageJsonConfig[PACKAGE_JSON_CONFIG_KEY],
|
63 | projectRoot,
|
64 | )
|
65 | } catch (e) {
|
66 | debug(`error: ${e.code} ${e.message}`)
|
67 | }
|
68 | return DEFAULT_CONFIG
|
69 | }
|
70 |
|
71 | function generateScriptsForPackageJson() {
|
72 | return ` "scripts": {\n ...\n`
|
73 | .concat(
|
74 | Object.keys(PACKAGE_SCRIPTS)
|
75 | .map((key) => ` "${key}": "${PACKAGE_SCRIPTS[key]}"`)
|
76 | .join(',\n'),
|
77 | )
|
78 | .concat('\n }')
|
79 | }
|
80 |
|
81 | function generateConfigForPackageJson() {
|
82 | return ` "${PACKAGE_JSON_CONFIG_KEY}": {\n`
|
83 | .concat(
|
84 | Object.keys(DEFAULT_CONFIG)
|
85 | .map((key) => ` "${key}": "${DEFAULT_CONFIG[key]}"`)
|
86 | .join(',\n'),
|
87 | )
|
88 | .concat('\n }')
|
89 | }
|
90 |
|
91 | module.exports = {
|
92 | getConfig,
|
93 | findPackageJson,
|
94 | generateScriptsForPackageJson,
|
95 | generateConfigForPackageJson,
|
96 | PACKAGE_JSON_CONFIG_KEY,
|
97 | DEFAULT_CONFIG,
|
98 | PACKAGE_SCRIPTS,
|
99 | STORYBOOK_PORT,
|
100 | SPDT_DIR,
|
101 | }
|