Skip to content

Commit aa59717

Browse files
authored
Overhaul on upstream retrieval. (#27)
* Overhaul on upstream retrieval. * Add --release option to README. * More ES6 fairy dust.
1 parent ba6f6e3 commit aa59717

File tree

3 files changed

+76
-42
lines changed

3 files changed

+76
-42
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
This is a [Yeoman](http://yeoman.io) generator for [Particle](https://github.com/phase2/pattern-lab-starter), a modern design-system driven Drupal theme.
44

5+
## Install
6+
57
To install generator-pattern-lab-starter from npm, run:
68

79
```bash
810
npm install -g generator-pattern-lab-starter
911
```
1012

13+
## Usage
14+
1115
*Note that this template will generate files in the current directory, so be sure to change to a new directory first if you don't want to overwrite existing files.*
1216

1317
Finally, initiate the generator:
@@ -22,6 +26,10 @@ Extras can be installed with:
2226
yo pattern-lab-starter:extras
2327
```
2428

29+
### Options
30+
31+
* **release**: Run with `--release=<branch-tag-or-hash>` to override the default use of master branch.
32+
2533
## Docker-based Development
2634

2735
You can perform local development of this generator using our Docker integration.

generators/app/index.js

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use strict';
2-
var Generator = require('yeoman-generator');
3-
var myPrompts = require('./prompts.js');
4-
var chalk = require('chalk');
5-
var yosay = require('yosay');
6-
var path = require('path');
7-
var exec = require('child_process').execSync;
8-
var _ = require('lodash');
2+
3+
const Generator = require('yeoman-generator');
4+
const chalk = require('chalk');
5+
const exec = require('child_process').execSync;
6+
const fs = require('fs-extra');
7+
const path = require('path');
8+
const yosay = require('yosay');
9+
const _ = require('lodash');
10+
const myPrompts = require('./prompts.js');
11+
912
var options = {};
1013

1114
module.exports = Generator.extend({
@@ -18,8 +21,7 @@ module.exports = Generator.extend({
1821
'Welcome to the remarkable ' + chalk.red('PatternLabStarter') + ' generator! ' + this.pkg.version + '\nPlease be in the folder you want files in now.'
1922
));
2023
}
21-
//options.themeName = _.last(this.env.cwd.split('/')); // parent folder
22-
// options.themePath = '';
24+
2325
options = _.assign(options, this.options);
2426
},
2527

@@ -35,57 +37,80 @@ module.exports = Generator.extend({
3537
return this.prompt(prompts).then(function (props) {
3638
options = _.assign(options, props);
3739
});
38-
39-
// disabling this for now as pattern lab starter v8 doesn't really have drupal 7 or drupal 8 differences
40-
// this.composeWith('pattern-lab-starter:extras', {options: options}, {
41-
// local: path.resolve(__dirname, '../extras')
42-
// });
43-
4440
},
4541

4642
configuring: function () {
4743

4844
},
4945

5046
default: function () {
51-
var dest = options.themePath ? path.resolve(process.cwd(), options.themePath) : './';
47+
const release = options['release'] || 'master'
48+
const release_path = `${release}.tar.gz`;
49+
const compressed = _.last(_.split(release_path, '/'));
50+
const decompressed = _.replace('particle-' + release, '/', '-');
51+
const download_url = `https://github.com/phase2/particle/archive/${release_path}`;
52+
const dest = options.themePath ? path.resolve(process.cwd(), options.themePath) : './';
53+
const themeName = 'patternlab';
54+
const themePathFull = path.join(dest, themeName);
5255

53-
var cmd = [
54-
'rm -Rf master.tar.gz particle-master patternlab' + ' ' + dest + '/patternlab' ,
55-
'curl -OL https://github.com/phase2/particle/archive/master.tar.gz',
56-
'tar -xzf master.tar.gz',
57-
'mv particle-master patternlab',
58-
'rm master.tar.gz'
59-
].join(' && ');
56+
this.log(`Assembling your Pattern Lab Starter/Particle theme on version ${release}...`);
6057

6158
try {
62-
exec(cmd, {
59+
fs.removeSync(compressed);
60+
} catch (err) {
61+
if (err.code != 'ENOENT') {
62+
console.error(err);
63+
process.exit(2);
64+
}
65+
}
66+
try {
67+
fs.removeSync(decompressed)
68+
} catch (err) {
69+
if (err.code != 'ENOENT') {
70+
console.error(err);
71+
process.exit(2);
72+
}
73+
}
74+
try {
75+
fs.removeSync(themePathFull)
76+
} catch (err) {
77+
if (err.code != 'ENOENT') {
78+
console.error(err);
79+
process.exit(2);
80+
}
81+
}
82+
83+
// @todo replace tarball retrieval & extraction with a Node library.
84+
try {
85+
this.log(`Retrieving template from ${download_url}...`);
86+
exec([
87+
`curl --fail --silent -OL ${download_url}`,
88+
`tar -xzf ${compressed}`
89+
].join(' && '), {
6390
encoding: 'utf8'
6491
});
65-
} catch(error) {
66-
console.error('An error happened while trying to run this command: ');
67-
console.log(cmd);
68-
// console.log(error);
92+
} catch(err) {
93+
this.log.error('An error occurred running retrieving and extracting the template.');
6994
process.exit(1);
7095
}
96+
// Remove the successfully decompressed archive source.
97+
fs.removeSync(compressed);
7198

72-
if (options.themePath) {
73-
var dest = path.resolve(process.cwd(), options.themePath);
99+
if (!fs.existsSync(dest)) {
74100
try {
75-
exec('mkdir -p "' + dest + '"', {
76-
encoding: 'utf8'
77-
});
101+
fs.mkdirpSync(dest);
78102
} catch(error) {
79-
console.error('Could not "mkdir -p" the themePath.');
103+
this.log.error(`Could not create the theme path: ${error}`);
104+
process.exit(2);
80105
}
106+
}
81107

82-
try {
83-
exec('mv patternlab "' + dest + '"', {
84-
encoding: 'utf8'
85-
});
86-
} catch (error) {
87-
console.error('Could not move theme into themePath.');
88-
}
108+
try {
109+
fs.renameSync(decompressed, themePathFull)
110+
} catch(error) {
111+
console.error(error);
112+
this.log.error(`Could not move theme into position: ${error}`);
113+
process.exit(2);
89114
}
90115
},
91116

@@ -116,6 +141,6 @@ module.exports = Generator.extend({
116141
}
117142

118143
this.log(yosay(finalWords));
119-
console.log('If you don\'t see your prompt, try hitting enter.');
144+
this.log("If you don't see your prompt, try hitting enter.");
120145
}
121146
});

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
],
3030
"dependencies": {
3131
"chalk": "^1.1.3",
32+
"fs-extra": "^4.0.2",
3233
"lodash": "^4.17.4",
3334
"yeoman-generator": "^1.1.0",
3435
"yosay": "^1.2.1"

0 commit comments

Comments
 (0)