-
Notifications
You must be signed in to change notification settings - Fork 1
/
plopfile.ts
103 lines (98 loc) · 3.82 KB
/
plopfile.ts
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
// @ts-nocheck
export = factory;
import * as _ from 'lodash';
import Path from 'path';
import fs from 'fs';
import * as nodePlop from 'node-plop';
type PlopApi = ReturnType<typeof nodePlop['default']>;
function factory(plop: PlopApi) {
const workspaceTemplatePath = Path.join(__dirname, 'personal-monorepo.template.code-workspace');
const workspacePath = Path.join(__dirname, 'personal-monorepo.code-workspace');
const workspaceTemplate = JSON.parse(fs.readFileSync(workspaceTemplatePath, 'utf8'));
let workspace: any = {folders: []};
try {
workspace = JSON.parse(fs.readFileSync(workspacePath, 'utf8'));
} catch {}
type Folder = {name: string, path: string};
const allFolders: Folder[] = [...workspaceTemplate.folders, ...workspaceTemplate._folders];
const focusedFolders: Folder[] = workspace.folders;
const allChoices = allFolders.map(v => {
return {
name: v.name,
value: v
};
});
const defaultChoices = focusedFolders.map(value => _.find(allFolders, {path: value.path}));
plop.setGenerator('focus', {
description: 'Create focused workspace.',
prompts: [{
type: 'checkbox',
name: 'packages',
choices: allChoices,
default: defaultChoices,
pageSize: 30
}],
actions: [(_answers, config, plopfileApi) => {
const answers = _answers as {packages: Folder[]};
fs.writeFileSync(workspacePath, JSON.stringify(
{
...workspaceTemplate,
folders: [
{
"name": "<empty>",
"path": "<empty>"
},
...answers.packages
]
},
null, 2
));
return `Updated ${ workspacePath }`;
}]
});
plop.setGenerator('new-package', {
description: 'Create a new package',
prompts: [{
type: 'input',
name: 'name',
message: 'Package name'
}],
actions: [
{
type: 'addMany',
base: 'packages/__template__',
templateFiles: 'packages/__template__/**',
globOptions: {dot: true},
destination: 'packages/{{name}}'
},
() => `Don't forget to run 'yarn fixup' after this.`
]
})
}
// import { patchJsonFile } from "../packages/scripting-core/src/core";
// import {sortBy, difference, pick} from 'lodash';
// import Path from 'path';
// import { workspaceFilename } from './fixup';
// import assert from 'assert';
// const [action, ..._focusOn] = process.argv.slice(2) as ['add' | 'rm' | 'reset', ...string[]];
// const targets = _focusOn.map(v => Path.basename(v));
// assert(['add', 'rm', 'reset'].includes(action));
// const always = ['__ROOT__', '__template__'];
// type Named = {name: string}
// patchJsonFile(workspaceFilename, (pkg: {folders: Named[], _folders: Named[]}) => {
// const {folders = [], _folders = []} = pkg;
// const allFolders = sortBy([...folders, ..._folders], v => v.name);
// function shouldInclude(folder: string) {
// const isTarget = targets.includes(folder);
// if(action === 'rm' && isTarget) return false;
// return always.includes(folder) || targets.includes(folder) || (action === 'add' && folders.some(v => v.name === folder));
// }
// if(targets.length) {
// pkg.folders = allFolders.filter(v => shouldInclude(v.name));
// pkg._folders = difference(allFolders, pkg.folders);
// } else {
// pkg.folders = allFolders;
// pkg._folders = [];
// }
// console.dir({shown: pkg.folders.map(v => v.name), hidden: pkg._folders.map(v => v.name)});
// });