-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathdspublisher-init.js
75 lines (60 loc) · 2.52 KB
/
dspublisher-init.js
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
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const ROOT = path.resolve(__dirname, '..');
const TEMPLATES = path.resolve(__dirname, 'dspublisher-init-templates');
const RELATIVE_THEME_PATH = `dspublisher/docs-theme`;
function copyTemplate(templateName, targetPath, replacements = {}) {
const content = fs.readFileSync(path.resolve(TEMPLATES, templateName), 'utf-8');
const newContent = Object.keys(replacements).reduce((c, key) => {
return c.split(`{{${key}}}`).join(replacements[key]);
}, content);
fs.writeFileSync(targetPath, newContent);
}
function appendFrontMatter(articlePath, frontMatter) {
const tokens = fs.readFileSync(articlePath, 'utf-8').split('---');
tokens[1] = tokens[1] + frontMatter;
fs.writeFileSync(articlePath, tokens.join('---'));
}
module.exports = async function (config) {
// Mark the repo to use sparsecheckout
execSync('git config core.sparsecheckout true', { cwd: ROOT, stdio: 'inherit' });
// sparse-checkout file
copyTemplate('dsp-sparse-checkout', path.resolve(ROOT, '.git/info/sparse-checkout'));
// .env file for dspublisher
copyTemplate('dsp-env', path.resolve(ROOT, 'dspublisher/.env'), {
title: config.dsName,
themePath: RELATIVE_THEME_PATH,
});
// Root index file under ds
copyTemplate('dsp-index.asciidoc', path.resolve(ROOT, 'articles/ds/index.asciidoc'), {
title: config.dsName || 'Design System',
});
// 404 page
copyTemplate('dsp-404.asciidoc', path.resolve(ROOT, 'articles/ds/404.asciidoc'));
// custom docs theme
const absoluteThemePath = path.resolve(ROOT, RELATIVE_THEME_PATH);
if (!fs.existsSync(absoluteThemePath)) {
fs.mkdirSync(absoluteThemePath);
}
fs.writeFileSync(path.resolve(absoluteThemePath, 'init.js'), '');
fs.writeFileSync(path.resolve(absoluteThemePath, 'header.ts'), '');
copyTemplate('dsp-global.css', path.resolve(absoluteThemePath, 'global.css'));
// CVDL license file
copyTemplate('dsp-license-cvdl', path.resolve(ROOT, 'LICENSE'));
// Readme
copyTemplate('dsp-readme.md', path.resolve(ROOT, 'README.md'), {
title: config.dsName || 'Design System',
});
// Have components / foundation expanded by default
appendFrontMatter(
path.resolve(ROOT, 'articles/ds/components/index.asciidoc'),
'section-nav: expanded\n'
);
appendFrontMatter(
path.resolve(ROOT, 'articles/ds/foundation/index.asciidoc'),
'section-nav: expanded\n'
);
// Update working directory
execSync('git read-tree -m -u HEAD', { cwd: ROOT, stdio: 'inherit' });
};