Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit 63fdc2d

Browse files
committed
Complete async rewrite. Added e2e tests
1 parent c84dd70 commit 63fdc2d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+656
-595
lines changed

bin/export.js renamed to bin/archive.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
'use strict';
2-
32
const fs = require('fs-promise');
43
const path = require('path');
54
const Archiver = require('archiver');
6-
const isValidConfig = require('./is_valid_config');
5+
const isValidConfig = require('./validate-config');
76
const debug = require('./utils').debug;
87

98
/**
109
* @func exportPatterns
1110
* @desc Exports the patterns into the patternExportDirectory.
12-
* @param {object} [config] - The passed PatternLab config.
11+
* @param {object} config - The passed PatternLab config.
1312
*/
1413
function exportPatterns(config) {
1514
if (!isValidConfig) throw new TypeError('export→Expects config not to be empty OR of type object if not empty.');

bin/ask.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
const inquirer = require('inquirer');
3+
const wrapsAsync = require('./utils').wrapAsync;
4+
const confirmSetup = require('./inquiries/confirm');
5+
const editionSetup = require('./inquiries/edition');
6+
const starterkitSetup = require('./inquiries/starterkit');
7+
const ask = inquirer.prompt;
8+
9+
/**
10+
* @func init
11+
* @desc Initiates a PatternLab project by getting user input through inquiry. Scaffolds the project and download mandatory files
12+
* @param {object} options - Options passed in from CLI
13+
* @param {boolean} options.force - Flag whether to force install in existing project directory. May overwrite stuff.
14+
*/
15+
const init = options => wrapsAsync(function*() {
16+
/**
17+
* @property {string} project_root="./" - Path to the project root directory
18+
* @property {string|Symbol} edition - The name of the edition npm package or a Symbol for no install
19+
*/
20+
const editionAnswers = yield ask(editionSetup);
21+
22+
/**
23+
* @property {object|Symbol} starterkit - The name of a starterkit npm package or a Symbol for no install
24+
*/
25+
const starterkitAnswers = yield ask(starterkitSetup);
26+
27+
/**
28+
* @property {boolean} confirm - A bool hold the confirmation status
29+
*/
30+
const confirmation = yield ask(confirmSetup);
31+
32+
// IF we have no confirmation we start all over again.
33+
if (!confirmation.confirm) return init(options);
34+
35+
return {
36+
// Destructure the answers
37+
projectDir: editionAnswers.project_root,
38+
edition: editionAnswers.edition !== false ? editionAnswers.edition : '',
39+
starterkit: starterkitAnswers.starterkit !== false ? starterkitAnswers.starterkit : ''
40+
};
41+
});
42+
43+
module.exports = init;

bin/build.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
2-
32
const pl = require('patternlab-node');
43
const debug = require('./utils').debug;
5-
const isValidConfig = require('./is_valid_config');
4+
const isValidConfig = require('./validate-config');
65

76
/**
87
* @func build

bin/check_args.js renamed to bin/check-args.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
32
const error = require('./utils').error;
43

54
/**
@@ -12,9 +11,8 @@ const error = require('./utils').error;
1211
function checkArgs(cmd) {
1312
/**
1413
* An immutable whitelist to check incoming <cmd> against
15-
* TODO: Need to complete the whitelist and add note to main file when adding new commands
1614
*/
17-
const CMD_WHITELIST = Object.freeze(['build', 'compile', 'init', 'export', 'serve', 'browse']);
15+
const CMD_WHITELIST = Object.freeze(['browse', 'build', 'compile', 'init', 'export', 'serve']);
1816

1917
// Check if a <cmd> was specified. If not → bail out
2018
if (typeof cmd === 'undefined') {

bin/check-overwrites.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
const path = require('path');
3+
const exists = require('path-exists');
4+
const debug = require('./utils').debug;
5+
6+
/**
7+
* @func checkOverwrites
8+
* @desc Checks whether a path exists and if it should be replaced.
9+
* @param {string} input - User input from inquirer.
10+
* @return {boolean} - Returns true if all went good.
11+
*/
12+
function checkOverwrites(input) {
13+
// Checks if project root exists to avoid overwriting.
14+
const inputPath = path.resolve(input);
15+
if (exists.sync(inputPath)) {
16+
// TODO: We might wanna add some more checks here. Currently CLI only displays a warning.
17+
debug(`\n patternlab→init: The directory (${inputPath}) already exists and is not empty. Proceed on your own risk.`);
18+
}
19+
return true;
20+
}
21+
22+
module.exports = checkOverwrites;

bin/cli-actions/build.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const buildPatterns = require('../build');
3+
const copyFiles = require('../copy-source-files');
4+
const resolveConfig = require('../resolve-config');
5+
const wrapAsync = require('../utils').wrapAsync;
6+
7+
const build = options => wrapAsync(function*() {
8+
const config = yield resolveConfig(options.parent.config);
9+
yield copyFiles(config.paths);
10+
buildPatterns(config, options);
11+
});
12+
13+
module.exports = build;

bin/cli-actions/export.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
const archive = require('../archive');
3+
const resolveConfig = require('../resolve-config');
4+
const wrapAsync = require('../utils').wrapAsync;
5+
6+
const _export = options => wrapAsync(function*() {
7+
const config = yield resolveConfig(options.parent.config);
8+
archive(config);
9+
});
10+
11+
module.exports = _export;

bin/cli-actions/help.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
module.exports = () => {
3+
/* eslint-disable */
4+
console.log(' Examples:');
5+
console.log('');
6+
console.log(' $ patternlab init # Initialize a PatternLab project.');
7+
console.log(' $ patternlab <cmd> # Builds the PatternLab from the current dir');
8+
console.log(' $ patternlab <cmd> --config <path/to/patternlab-config> # PatternLab from a config in a specified directory');
9+
console.log('');
10+
/* eslint-enable */
11+
};

bin/cli-actions/init.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const ask = require('../ask');
3+
const scaffold = require('../scaffold');
4+
const installEdition = require('../install-edition');
5+
const installStarterkit = require('../install-starterkit');
6+
const defaultPatternlabConfig = require('../default-config');
7+
const replaceConfigPaths = require('../replace-config');
8+
const path = require('path');
9+
const wrapAsync = require('../utils').wrapAsync;
10+
const writeJsonAsync = require('../utils').writeJsonAsync;
11+
12+
const init = options => wrapAsync(function*() {
13+
const sourceDir = 'source';
14+
const publicDir = 'public';
15+
const exportDir = 'pattern_exports';
16+
const answers = options.projectDir ? options : yield ask(options);
17+
const projectDir = answers.projectDir || './';
18+
const edition = answers.edition;
19+
const starterkit = answers.starterkit;
20+
21+
/**
22+
* Process the init routines
23+
* 1 Replace config paths
24+
* 2. Scaffold the folder structure
25+
* 3. If `edition` is present:
26+
* 3.1 Install edition
27+
* 3.2 Reassign adjustedconfig
28+
* 3. If `starterkit` is present
29+
* 3.1 Install it
30+
* 3.2 Copy over the mandatory starterkit files to sourceDir
31+
* 4. Check for starterkit and install it
32+
* 5. Save patternlab-config.json in projectDir
33+
*/
34+
let patternlabConfig = replaceConfigPaths(defaultPatternlabConfig, projectDir, sourceDir, publicDir, exportDir); // 1
35+
36+
yield scaffold(projectDir, sourceDir, publicDir, exportDir); // 2
37+
38+
if (edition) {
39+
const newConf = yield installEdition(edition, patternlabConfig); // 3.1
40+
patternlabConfig = Object.assign(patternlabConfig, newConf); // 3.2
41+
}
42+
if (starterkit) yield installStarterkit(starterkit, patternlabConfig); // 4
43+
yield writeJsonAsync(path.resolve(projectDir, 'patternlab-config.json'), patternlabConfig); // 4.2
44+
45+
// Finally :>
46+
if (!edition && !starterkit) {
47+
console.log(`patternlab→init: You haven't picked an edition nor a starterkit. PatternLab won't work without those. Please add them manually.`); // eslint-disable-line
48+
} else {
49+
console.log(`patternlab→init: Additional packages installed - ${edition ? 'edition: ' + edition : ''} ${starterkit ? ', starterkit: ' + starterkit.name : ''}`); // eslint-disable-line
50+
}
51+
console.log(`patternlab→init: Yay ☺. PatternLab Node was successfully initialised in ${projectDir}`); // eslint-disable-line
52+
return true;
53+
});
54+
55+
module.exports = init;

bin/cli-actions/serve.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
const resolveConfig = require('../resolve-config');
3+
const preview = require('../preview');
4+
const wrapAsync = require('../utils').wrapAsync;
5+
6+
const serve = options => wrapAsync(function*() {
7+
const config = yield resolveConfig(options.parent.config);
8+
preview(config);
9+
});
10+
11+
module.exports = serve;

0 commit comments

Comments
 (0)