Skip to content

Commit 81d7553

Browse files
authored
🔀 Merge pull request #382 from richardfrost/more_build_fixes
Keep separate dev and release build states
2 parents 4610b52 + 726a11b commit 81d7553

File tree

9 files changed

+146
-134
lines changed

9 files changed

+146
-134
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
# Ignore git-filter scripts (clean/smudge)
55
bin/git-filters/*.js
66

7-
# Build state file
8-
/.build.json
9-
/.release.json
7+
# Build state files
8+
/.build.*json
109

1110
# Packaged extensions
1211
/dist/*

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,17 @@ npm run build
6666
Once the extension has been built, you can load the unpacked extension (found in `dist/`) in your browser.
6767

6868
### Stages
69-
| Stage | Output | Description |
70-
|---------|--------------------------|---------------------------------------------------|
69+
| Stage | Output | Description |
70+
|---------|------------------------|---------------------------------------------------|
7171
| build | `dist/` | Build/compile the extension for local development |
7272
| package | `extension-target.zip` | Package the files for the target browser |
7373
| release | `extension-target.zip` | Create an official release for a target browser |
7474

7575
### Targets
7676
| Target | Browser |
7777
|---------|--------------------|
78-
| v3* | Chrome, Edge, etc. |
79-
| v2 | Chrome, Edge, etc. |
78+
| v3 | Chrome, Edge, etc. |
79+
| v2* | Chrome, Edge, etc. |
8080
| firefox | Firefox |
8181
| safari | Safari (MacOS/iOS) |
8282

@@ -97,13 +97,15 @@ For all scripts, please see `package.json`.
9797

9898
### State files
9999
The state files hold the details about the current build. These files are managed by `bin/prebuild.mjs`.
100-
101100
- `.build.json`
102-
- Current development target details
103-
- This is used for active development, and allows the developer to run simple commands such as `npm run build` to rebuild the project for the target specified in the file
104-
- `.release.json`
105-
- Current release target details
106-
- Takes precedence over `.build.json`, but gets removed after release is finished
101+
- Active build state file that is referenced when building/packaging/releasing
102+
- Gets replaced by the dev or release build files outlined below
103+
- `.build.dev.json`
104+
- Holds the development build details and allows the developer to omit the target from commands such as `npm run build` to rebuild the project for the target specified in the file
105+
- Overwrites `.build.json` when `--release` is **not** passed to `bin/prebuild.mjs`
106+
- `.build.release.json`
107+
- Holds the release build details
108+
- Overwrites `.build.json` when `--release` **is** passed to `bin/prebuild.mjs`
107109

108110
#### Details contained in state files:
109111
- `config`: Overrides for the target

bin/clean.mjs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-console */
2-
import fse from 'fs-extra';
32
import path from 'path';
3+
import { parseArgv, removeFiles } from './lib.mjs';
44

55
const built = [
66
path.join('extension'),
@@ -12,50 +12,32 @@ const dist = [
1212
path.join('dist-lib'),
1313
];
1414

15-
const release = [
16-
path.join('./.release.json'),
17-
];
18-
1915
const test = [
2016
path.join('test', 'built'),
2117
];
2218

23-
function clean(items) {
24-
items.forEach((item) => {
25-
console.log(`Cleaning ${item}...`);
26-
fse.removeSync(item);
27-
});
28-
}
29-
3019
function main() {
3120
try {
32-
// argv[0] = process (node)
33-
// argv[1] = script (this file)
34-
// argv[2] = first argument
35-
if (process.argv.length >= 2) {
36-
let args = process.argv.slice(2);
37-
if (args.length == 0 || args.includes('--all')) {
38-
args = ['--built', '--dist', '--release', '--test'];
21+
const argv = parseArgv(process.argv);
22+
if (argv.count >= 2) {
23+
if (argv.arguments.length == 0 || argv.arguments.includes('--all')) {
24+
argv.arguments = ['--built', '--dist', '--test'];
3925
}
4026

4127
let toRemove = [];
42-
if (args.includes('--built')) {
28+
if (argv.arguments.includes('--built')) {
4329
toRemove = toRemove.concat(built);
4430
}
4531

46-
if (args.includes('--dist')) {
32+
if (argv.arguments.includes('--dist')) {
4733
toRemove = toRemove.concat(dist);
4834
}
4935

50-
if (args.includes('--release')) {
51-
toRemove = toRemove.concat(release);
52-
}
53-
54-
if (args.includes('--test')) {
36+
if (argv.arguments.includes('--test')) {
5537
toRemove = toRemove.concat(test);
5638
}
5739

58-
clean(toRemove);
40+
removeFiles(toRemove);
5941
} else {
6042
usage();
6143
}
@@ -70,7 +52,6 @@ function usage() {
7052
npm run clean
7153
npm run clean:built
7254
npm run clean:dist
73-
npm run clean:release
7455
npm run clean:test
7556
`);
7657
}

bin/lib.mjs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fse from 'fs-extra';
2+
import path from 'path';
3+
4+
// Paths are relative from the project root
5+
// Should be correct when running with `npm run ...`
6+
export const buildFilePath = path.join('.build.json');
7+
export const devBuildFilePath = path.join('.build.dev.json');
8+
export const distManifestPath = path.join('dist', 'manifest.json');
9+
export const releaseBuildFilePath = path.join('.build.release.json');
10+
export const srcManifestPath = path.join('src', 'static', 'manifest.json');
11+
12+
export function loadJSONFile(file) {
13+
return JSON.parse(fse.readFileSync(file));
14+
}
15+
16+
export function parseArgv(argv) {
17+
const parsed = {};
18+
parsed.count = argv.length;
19+
parsed.process = argv[0]; // process (node)
20+
parsed.script = argv[1]; // script
21+
parsed.arguments = argv.slice(2);
22+
return parsed;
23+
}
24+
25+
export function removeFiles(files, silent = false) {
26+
if (typeof files === 'string') {
27+
files = [files];
28+
}
29+
30+
files.forEach((file) => {
31+
if (!silent) {
32+
// eslint-disable-next-line no-console
33+
console.log(`Removing ${file}`);
34+
}
35+
fse.removeSync(file);
36+
});
37+
}
38+
39+
export function writeJSONFile(file, object) {
40+
const content = JSON.stringify(object, null, 2);
41+
fse.writeFileSync(file, content);
42+
}

bin/package-extension.mjs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,24 @@
11
/* eslint-disable no-console */
2-
import fse from 'fs-extra';
3-
import path from 'path';
42
import AdmZip from 'adm-zip';
3+
import { buildFilePath, loadJSONFile, removeFiles } from './lib.mjs';
54

65
let buildData;
7-
const buildDataPath = path.join('.build.json');
86
const dist = './dist/';
9-
const releaseFilePath = path.join('.release.json');
107

118
function buildArchive() {
129
const zip = new AdmZip();
1310
zip.addLocalFolder(dist, null);
1411
return zip;
1512
}
1613

17-
function loadJSONFile(file) {
18-
try {
19-
return JSON.parse(fse.readFileSync(file));
20-
} catch (err) {
21-
console.error(err.message);
22-
throw err;
23-
}
24-
}
25-
2614
function main() {
2715
try {
28-
// Load .release.json if present, otherwise load .build.json
29-
if (fse.existsSync(releaseFilePath)) {
30-
buildData = loadJSONFile(releaseFilePath);
31-
} else {
32-
buildData = loadJSONFile(buildDataPath);
33-
}
16+
buildData = loadJSONFile(buildFilePath);
3417

3518
const zip = buildArchive();
3619
const packagePath = `./${zipName()}.zip`;
20+
removeFiles(packagePath, true);
3721
console.log(`Building ${packagePath}`);
38-
fse.removeSync(packagePath);
3922
zip.writeZip(packagePath);
4023
} catch (err) {
4124
console.error(err.message);

bin/package-source.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/* eslint-disable no-console */
2-
import fse from 'fs-extra';
32
import Path from 'path';
43
import AdmZip from 'adm-zip';
4+
import { removeFiles } from './lib.mjs';
55

6-
// Required due to bundled code
6+
// Required for Firefox due to bundled code
77
function packageSource() {
8-
fse.removeSync('./extension-source.zip');
8+
removeFiles('./extension-source.zip', true);
99
console.log('Building ./extension-source.zip');
10-
console.log('Build from source: npm install && npm run build:bookmarklet && npm run package:firefox');
10+
console.log('Build from source: npm install && npm run package:bookmarklet && npm run package:firefox');
1111
console.log(' Unpacked: ./dist');
1212
console.log(' Packed: ./extension-firefox.zip');
1313

bin/postbuild.mjs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,36 @@
22
/* eslint-disable no-console */
33
import fse from 'fs-extra';
44
import path from 'path';
5+
import {
6+
buildFilePath,
7+
distManifestPath,
8+
loadJSONFile,
9+
removeFiles,
10+
srcManifestPath,
11+
writeJSONFile
12+
} from './lib.mjs';
513

614
let buildData;
7-
const buildDataPath = path.join('.build.json');
8-
const manifestPath = path.join('dist', 'manifest.json');
9-
const releaseFilePath = path.join('.release.json');
10-
const srcManifestPath = path.join('src', 'static', 'manifest.json');
1115

1216
function common() {
1317
handleManifestVersion();
1418
handleVersion();
1519
}
1620

1721
function firefoxBuild() {
18-
const manifest = loadJSONFile(manifestPath);
22+
const manifest = loadJSONFile(distManifestPath);
1923
manifest.applications = {
2024
gecko: {
2125
id: '{853d1586-e2ab-4387-a7fd-1f7f894d2651}'
2226
}
2327
};
2428
manifest.background.persistent = true; // Event pages are not currently supported.
25-
writeJSONFile(manifestPath, manifest);
29+
writeJSONFile(distManifestPath, manifest);
2630
}
2731

2832
function handleManifestVersion() {
2933
if (buildData.manifestVersion == 2) {
30-
const manifest = loadJSONFile(manifestPath);
34+
const manifest = loadJSONFile(distManifestPath);
3135
manifest.action = undefined;
3236
manifest.manifest_version = buildData.manifestVersion;
3337
manifest.options_ui = {
@@ -48,17 +52,17 @@ function handleManifestVersion() {
4852
default_title: 'Advanced Profanity Filter',
4953
};
5054
manifest.web_accessible_resources = ['audio/*.mp3'];
51-
writeJSONFile(manifestPath, manifest);
55+
writeJSONFile(distManifestPath, manifest);
5256
}
5357
}
5458

5559
function handleVersion() {
56-
const manifest = loadJSONFile(manifestPath);
60+
const manifest = loadJSONFile(distManifestPath);
5761

5862
if (manifest.version != buildData.version) {
5963
console.log(`Updating manifest.json version (${manifest.version} -> ${buildData.version})`);
6064
manifest.version = buildData.version;
61-
writeJSONFile(manifestPath, manifest);
65+
writeJSONFile(distManifestPath, manifest);
6266

6367
// Update source manfiest.json
6468
const srcManifest = loadJSONFile(srcManifestPath);
@@ -67,17 +71,8 @@ function handleVersion() {
6771
}
6872
}
6973

70-
function loadJSONFile(file) {
71-
return JSON.parse(fse.readFileSync(file));
72-
}
73-
7474
function main() {
75-
// Load .release.json if present, otherwise load .build.json
76-
if (fse.existsSync(releaseFilePath)) {
77-
buildData = loadJSONFile(releaseFilePath);
78-
} else {
79-
buildData = loadJSONFile(buildDataPath);
80-
}
75+
buildData = loadJSONFile(buildFilePath);
8176

8277
// Perform postbuild actions
8378
common();
@@ -91,17 +86,6 @@ function main() {
9186
}
9287
}
9388

94-
function removeFiles(files) {
95-
if (typeof files === 'string') {
96-
files = [files];
97-
}
98-
99-
files.forEach((file) => {
100-
console.log(`Removing ${file}`);
101-
fse.removeSync(file);
102-
});
103-
}
104-
10589
function removeOptionPageBookmarklet() {
10690
console.log("Removing Bookmarklet tab from Option's page");
10791
const optionPage = path.join('dist', 'optionPage.html');
@@ -140,9 +124,4 @@ function safariBuild() {
140124
removeFiles(files);
141125
}
142126

143-
function writeJSONFile(file, object) {
144-
const content = JSON.stringify(object, null, 2);
145-
fse.writeFileSync(file, content);
146-
}
147-
148127
main();

0 commit comments

Comments
 (0)