forked from jaredpalmer/razzle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-examples.js
More file actions
194 lines (174 loc) · 5.74 KB
/
Copy pathupdate-examples.js
File metadata and controls
194 lines (174 loc) · 5.74 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
const execa = require('execa');
const util = require('util');
const glob = util.promisify(require('glob'));
const updateSection = require('update-section');
const fs = require('fs-extra');
const path = require('path');
const rootDir = process.cwd();
var startInstall =
'<!-- START install generated instructions please keep comment here to allow auto update -->\n' +
"<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN yarn update-examples TO UPDATE -->",
endInstall =
'<!-- END install generated instructions please keep comment here to allow auto update -->';
function matchesStartInstall(line) {
return /<!-- START install /.test(line);
}
function matchesEndInstall(line) {
return /<!-- END install /.test(line);
}
function updateInstallSection(
example,
readme,
branch,
releaseBranches,
preReleaseBranches
) {
fs.readFile(readme).then(content => {
let update = '';
if (releaseBranches.includes(branch)) {
const preReleaseBranch = preReleaseBranches.includes(branch);
const tag = preReleaseBranch ? `@${branch}` : '';
const info = preReleaseBranch
? `\nThis is the ${branch} release documentation for this example\n\n`
: '';
update = `${info}Create and start the example:\n\n`;
update += `\`\`\`bash\nnpx create-razzle-app${tag} --example ${example} ${example}\n\n`;
update += `cd ${example}\nyarn start\n\`\`\`\n`;
} else {
update =
'\nThis is the development documentation for this example\n\nClone the `razzle` repository:\n\n';
update += `\`\`\`bash\ngit clone https://github.com/jaredpalmer/razzle.git\n\n`;
update += `cd razzle\nyarn install --frozen-lockfile --ignore-engines --network-timeout 30000\n\`\`\`\n\n`;
update += `Create and start the example:\n\n`;
update += `\`\`\`bash\nnode -e 'require("./test/fixtures/util").setupStageWithExample("${example}", "${example}", symlink=false, yarnlink=true, install=true, test=false);'\n\n`;
update += `cd ${example}\nyarn start\n\`\`\`\n`;
}
const contentString = content.toString();
if (matchesStartInstall(contentString)) {
const updated = updateSection(
contentString,
startInstall + update + endInstall,
matchesStartInstall,
matchesEndInstall
);
return fs.writeFile(readme, updated);
}
});
}
function updatePackageJson(
example,
packageJson,
branch,
dependencyVersions,
) {
fs.pathExists(packageJson).then(async exists => {
if (exists) {
const packageJsonData = JSON.parse(await fs.readFile(packageJson));
const newPackageJsonData = ['dependencies', 'devDependencies'].reduce(
(acc, depType) => {
if (acc[depType]) {
acc[depType] = Object.keys(acc[depType]).reduce((depsAcc, dep) => {
if (dependencyVersions[dep]) {
depsAcc[dep] = dependencyVersions[dep];
}
return depsAcc;
}, acc[depType]);
}
return acc;
},
packageJsonData
);
return fs.writeFile(
packageJson,
JSON.stringify(newPackageJsonData, null, ' ') + "\n"
);
}
});
}
execa('git', ['rev-parse', '--abbrev-ref', 'HEAD'], { shell: true }).then(
async ({ stdout }) => {
const branch = stdout.split('\n')[0];
const lernaJson = JSON.parse(
await fs.readFile(path.join(rootDir, 'lerna.json'))
);
const exampleDependencyVersions = JSON.parse(
await fs.readFile(
path.join(rootDir, 'scripts', 'exampleDependencyVersions.json')
)
);
const packageJsons = (
await Promise.all(
lernaJson.packages.map(item => glob(item + '/package.json'))
)
).flat();
const packageVersions = Object.fromEntries(
(
await Promise.all(
packageJsons.map(async item => JSON.parse(await fs.readFile(item)))
)
).map(item => [item.name, item.version])
);
const dependencyVersions = {
...exampleDependencyVersions,
...packageVersions,
};
const releaseBranches = lernaJson.command.publish.allowBranch;
const preReleaseBranches = releaseBranches.filter(b => b !== 'master');
fs.readdir(path.join(rootDir, 'examples'), { withFileTypes: true }).then(
items => {
return items
.filter(item => item.isDirectory())
.map(item => {
updateInstallSection(
item.name,
path.join(rootDir, 'examples', item.name, 'README.md'),
branch,
releaseBranches,
preReleaseBranches
);
updatePackageJson(
item.name,
path.join(rootDir, 'examples', item.name, 'package.json'),
branch,
dependencyVersions
);
});
}
);
updatePackageJson(
'default',
path.join(
rootDir,
'packages',
'create-razzle-app',
'templates',
'default',
'package.json'
),
branch,
dependencyVersions
);
const loadExamplePath =
'packages/create-razzle-app/lib/utils/load-example.js';
fs.readFile(loadExamplePath).then(content => {
const updated = content
.toString()
.replace(
/(?=const branch.*?yarn update-examples)(.*?)'.*?'/,
"$1'" + branch + "'"
);
return fs.writeFile(loadExamplePath, updated);
});
const installExamplePath = 'packages/create-razzle-app/lib/index.js';
fs.readFile(installExamplePath).then(content => {
const updated = content
.toString()
.replace(
/(?=const branch.*?yarn update-examples)(.*?)'.*?'/,
"$1'" + branch + "'"
);
return fs.writeFile(installExamplePath, updated);
});
}
);