Skip to content

Commit 01012ee

Browse files
committed
Add workspace scripts for cloning and updating
1 parent 0abbfdd commit 01012ee

Some content is hidden

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

50 files changed

+5968
-101
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
node_modules
22
package-lock.json
3-
yarn.json
43

54
# yarn
65
.pnp.*
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/* eslint-disable no-underscore-dangle */
2+
const Generator = require('yeoman-generator');
3+
const { merge, template } = require('lodash');
4+
const path = require('path');
5+
const glob = require('glob').sync;
6+
const fs = require('fs-extra');
7+
const GeneratorUtils = require('./utils/GeneratorUtils');
8+
9+
class BasePhoveaGenerator extends Generator {
10+
/**
11+
* Modify package.json by passing the configuration.
12+
*
13+
* @param {object} config Current configuration
14+
* @param {*} unset
15+
* @param {*} extra
16+
* @param {*} replaceExtra
17+
* @param {string} cwd The directory from which the generator is being called, i.e., `tdp_core/`.
18+
* If cwd is provided than the `package.json` is going to be written to that subdirectory, otherwise to the current directory.
19+
*/
20+
_patchPackageJSON(config, unset, extra, replaceExtra, cwd = '') {
21+
const pkg = this.fs.readJSON(this.destinationPath(`${cwd}package.json`), {});
22+
let pkgPatch;
23+
if (fs.existsSync(this.templatePath('package.tmpl.json'))) {
24+
pkgPatch = JSON.parse(template(this.fs.read(this.templatePath('package.tmpl.json')))(config));
25+
} else {
26+
pkgPatch = {};
27+
}
28+
merge(pkg, pkgPatch);
29+
if (replaceExtra && extra) {
30+
Object.assign(pkg, extra);
31+
} else {
32+
merge(pkg, extra || {});
33+
}
34+
35+
(unset || []).forEach((d) => delete pkg[d]);
36+
37+
this.fs.writeJSON(this.destinationPath(`${cwd}package.json`), pkg);
38+
}
39+
40+
/**
41+
* Copies the template files to the current directory or to a subdirectory if `cwd` is provided.
42+
* @param {object} config Current configuration
43+
* @param {*} withSamples
44+
* @param {string} cwd The directory from which the generator is being called, i.e., `tdp_core/`.
45+
* If `cwd` is provided than the `package.json` is going to be written to that subdirectory, otherwise to the current directory.
46+
*/
47+
_writeTemplates(config, withSamples, cwd = '') {
48+
const includeDot = {
49+
globOptions: {
50+
dot: true,
51+
},
52+
};
53+
54+
const pattern = GeneratorUtils.stringifyAble(config);
55+
56+
const copyTpl = (base, dbase, initializeOnce) => {
57+
// see https://github.com/SBoudrias/mem-fs-editor/issues/25
58+
// copyTpl doesn't support glob options
59+
const f = glob(`${base}/**/*`, {
60+
dot: true,
61+
nodir: true,
62+
});
63+
f.forEach((fi) => {
64+
const rel = path.relative(base, fi);
65+
if (!initializeOnce || !fs.existsSync(this.destinationPath(cwd + dbase + rel))) {
66+
this.fs.copyTpl(fi, this.destinationPath(cwd + dbase + rel), pattern);
67+
}
68+
});
69+
};
70+
71+
const copy = (prefix) => {
72+
if (fs.existsSync(this.templatePath(`${prefix}plain`))) {
73+
this.fs.copy(this.templatePath(`${prefix}plain/**/*`), this.destinationPath(cwd), includeDot);
74+
}
75+
76+
const plainTemplatePath = this.templatePath(`${prefix}plain_initialize_once`);
77+
if (fs.existsSync(plainTemplatePath)) {
78+
copyTpl(plainTemplatePath, '', true);
79+
}
80+
81+
copyTpl(this.templatePath(`${prefix}processed`), '', false);
82+
83+
if (config.name) {
84+
if (fs.existsSync(this.templatePath(`${prefix}pluginname_plain`))) {
85+
this.fs.copy(this.templatePath(`${prefix}pluginname_plain/**/*`), this.destinationPath(`${cwd + config.name.toLowerCase()}/`), includeDot);
86+
}
87+
copyTpl(this.templatePath(`${prefix}pluginname_processed`), `${config.name.toLowerCase()}/`, false);
88+
}
89+
};
90+
copy('');
91+
if (withSamples) {
92+
copy('sample_');
93+
}
94+
}
95+
}
96+
97+
module.exports = BasePhoveaGenerator;
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/* eslint-disable no-shadow */
2+
/* eslint-disable no-param-reassign */
3+
/* eslint-disable no-underscore-dangle */
4+
const Base = require('yeoman-generator');
5+
const chalk = require('chalk');
6+
const RepoUtils = require('../utils/RepoUtils');
7+
const NpmUtils = require('../utils/NpmUtils');
8+
const SpawnUtils = require('../utils/SpawnUtils');
9+
10+
/**
11+
* Clone a given repository and supports version ranges for git tags.
12+
* @see https://docs.npmjs.com/misc/semver#advanced-range-syntax
13+
*/
14+
class Generator extends Base {
15+
constructor(args, options) {
16+
super(args, options);
17+
18+
this.argument('repository', {
19+
alias: 'r',
20+
defaults: '',
21+
type: String,
22+
required: true,
23+
});
24+
25+
// Note for Windows Powershell
26+
// Using caret version range like `-b '^v2.0.0'` does not work!
27+
// Instead you must escape the caret sign as explained in
28+
// https://stackoverflow.com/a/5254713/940219
29+
// Correct version: `-b '^^^^v2.0.0'`
30+
this.option('branch', {
31+
alias: 'b',
32+
defaults: 'main',
33+
required: false,
34+
type: String,
35+
});
36+
37+
// Note for Windows Powershell
38+
// Using `-e '--depth 1'` does not work!
39+
// Instead add an additional space at the beginning
40+
// to the argument value: `-e ' --depth 1'`
41+
this.option('extras', {
42+
alias: 'e',
43+
defaults: '',
44+
type: String,
45+
});
46+
47+
// no prompt implemented
48+
this.option('cwd', {
49+
defaults: '',
50+
type: String,
51+
});
52+
53+
// no prompt implemented
54+
this.option('dir', {
55+
alias: 'd',
56+
defaults: '',
57+
type: String,
58+
});
59+
}
60+
61+
prompting() {
62+
return this.prompt([
63+
{
64+
type: 'input',
65+
name: 'repository',
66+
message: 'Repository URL',
67+
required: true,
68+
default: this.args.length > 0 ? this.args[0] : undefined,
69+
when: this.args.length === 0,
70+
validate: (d) => d.length > 0,
71+
},
72+
{
73+
type: 'input',
74+
name: 'branch',
75+
message: 'Git branch or tag',
76+
required: true,
77+
default: this.options.branch,
78+
when: this.options.branch === '',
79+
validate: (d) => d.length > 0,
80+
},
81+
{
82+
type: 'input',
83+
name: 'extras',
84+
message: 'Additional git clone parameters',
85+
required: false,
86+
default: this.options.extras || undefined,
87+
when: this.options.extras === undefined,
88+
},
89+
]).then((props) => {
90+
this.cwd = this.options.cwd || undefined;
91+
this.cloneDirName = this.options.dir === '' ? this.options.dir : ` ${this.options.dir}`; // add space at the beginning
92+
this.options.repository = props.repository || this.args[0];
93+
this.options.branch = props.branch || this.options.branch;
94+
this.options.extras = props.extras || this.options.extras;
95+
this.options.extras = this.options.extras === '' ? this.options.extras : ` ${this.options.extras}`; // add space at the beginning
96+
});
97+
}
98+
99+
writing() {
100+
return this._cloneRepo(this.options.repository, this.options.branch, this.options.extras, this.cloneDirName);
101+
}
102+
103+
_cloneRepo(repoUrl, branch, extras, cloneDirName) {
104+
if (!NpmUtils.isGitCommit(branch)) {
105+
// modify branch name, if it is an advance version tag
106+
// otherwise just use the branch name as it is
107+
if (NpmUtils.isAdvancedVersionTag(branch)) {
108+
this.log(chalk.white('found branch with version range'), chalk.green(branch), chalk.white('for'), chalk.green(repoUrl));
109+
110+
const line = `ls-remote --tags ${repoUrl}`;
111+
this.log(chalk.white('fetching possible version tags:'), `git ${line}`);
112+
const r = SpawnUtils.spawnSync('git', line.split(/ +/), this.cwd);
113+
114+
if (SpawnUtils.failed(r)) {
115+
this.log(chalk.red('failed to fetch list of tags from git repository'), `status code: ${r.status}`);
116+
this.log(r.stderr.toString());
117+
return SpawnUtils.abort(`failed to fetch list of tags from git repository - status code: ${r.status}`);
118+
}
119+
120+
const gitLog = r.stdout.toString();
121+
const gitVersions = NpmUtils.extractVersionsFromGitLog(gitLog);
122+
this.log(chalk.white('found the following version tags: '), gitVersions);
123+
124+
const highestVersion = NpmUtils.findHighestVersion(gitVersions, branch);
125+
if (!highestVersion) {
126+
this.log(chalk.red('failed to find git version tag for given version range'));
127+
return SpawnUtils.abort('failed to find git version tag for given version range');
128+
}
129+
130+
this.log(chalk.white('use version tag'), chalk.green(highestVersion), chalk.white('as branch name'));
131+
branch = highestVersion;
132+
}
133+
134+
const line = `clone -b ${branch}${extras || ''} ${repoUrl}${cloneDirName}`;
135+
this.log(chalk.white('clone repository:'), `git ${line}`);
136+
return SpawnUtils.spawnOrAbort('git', line.split(/ +/), this.cwd);
137+
}
138+
139+
// clone a specific commit
140+
const line = `clone ${extras || ''} ${repoUrl}${cloneDirName}`;
141+
this.log(chalk.white('clone repository:'), `git ${line}`);
142+
143+
return SpawnUtils.spawnOrAbort('git', line.split(/ +/), this.cwd).then(() => {
144+
const line = `checkout ${branch}`;
145+
this.log(chalk.white('checkout commit:'), `git ${line}`);
146+
let repoName = RepoUtils.simplifyRepoUrl(repoUrl);
147+
repoName = repoName.slice(repoName.lastIndexOf('/') + 1);
148+
const cwd = this.cwd ? `${this.cwd}/${repoName}` : repoName;
149+
return SpawnUtils.spawnOrAbort('git', line.split(/ +/), cwd);
150+
});
151+
}
152+
}
153+
154+
module.exports = Generator;

bin/commands/generator/index.js

Whitespace-only changes.

0 commit comments

Comments
 (0)