Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit fb4889c

Browse files
committedJun 10, 2020
build: added build & consolidated structure a bit
1 parent f53bcb9 commit fb4889c

10 files changed

+229
-28
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ dist
77
# when working with contributors
88
package-lock.json
99
yarn.lock
10+
chrome/build

‎build.js

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env node
2+
3+
// shamelessly copied from react devtools as it's doing exactly what i need.
4+
5+
const { execSync } = require("child_process");
6+
const { readFileSync, writeFileSync, createWriteStream } = require("fs");
7+
const { join } = require("path");
8+
const { copy, ensureDir, move, remove } = require("fs-extra");
9+
const archiver = require("archiver");
10+
const { getGitCommit } = require("./git");
11+
12+
// These files are copied along with Webpack-bundled files
13+
// to produce the final web extension
14+
const STATIC_FILES = ["icons", "pages", "lib"];
15+
16+
const preProcess = async (destinationPath, tempPath) => {
17+
await remove(destinationPath); // Clean up from previously completed builds
18+
await remove(tempPath); // Clean up from any previously failed builds
19+
await ensureDir(tempPath); // Create temp dir for this new build
20+
};
21+
22+
const build = async (tempPath, manifestPath) => {
23+
const binPath = join(tempPath, "bin");
24+
const zipPath = join(tempPath, "zip");
25+
26+
const webpackPath = join(__dirname, "node_modules", ".bin", "webpack");
27+
execSync(
28+
`${webpackPath} --config webpack.config.js --output-path ${binPath}`,
29+
{
30+
cwd: __dirname,
31+
env: process.env,
32+
stdio: "inherit",
33+
}
34+
);
35+
// execSync(
36+
// `${webpackPath} --config webpack.backend.js --output-path ${binPath}`,
37+
// {
38+
// cwd: __dirname,
39+
// env: process.env,
40+
// stdio: 'inherit',
41+
// },
42+
// );
43+
44+
// Make temp dir
45+
await ensureDir(zipPath);
46+
47+
const copiedManifestPath = join(zipPath, "manifest.json");
48+
49+
// Copy unbuilt source files to zip dir to be packaged:
50+
await copy(binPath, join(zipPath, "dist"));
51+
await copy(manifestPath, copiedManifestPath);
52+
await Promise.all(
53+
STATIC_FILES.map((file) => copy(join(__dirname, file), join(zipPath, file)))
54+
);
55+
56+
const commit = getGitCommit();
57+
const dateString = new Date().toLocaleDateString();
58+
const manifest = JSON.parse(readFileSync(copiedManifestPath).toString());
59+
const versionDateString = `${manifest.version} (${dateString})`;
60+
if (manifest.version_name) {
61+
// eslint-disable-next-line babel/camelcase
62+
manifest.version_name = versionDateString;
63+
}
64+
manifest.description += `\n\nCreated from revision ${commit} on ${dateString}.`;
65+
66+
writeFileSync(copiedManifestPath, JSON.stringify(manifest, null, 2));
67+
68+
// Pack the extension
69+
const archive = archiver("zip", { zlib: { level: 9 } });
70+
const zipStream = createWriteStream(
71+
join(tempPath, "TestingLibraryDevTools.zip")
72+
);
73+
await new Promise((resolve, reject) => {
74+
archive
75+
.directory(zipPath, false)
76+
.on("error", (err) => reject(err))
77+
.pipe(zipStream);
78+
archive.finalize();
79+
zipStream.on("close", () => resolve());
80+
});
81+
};
82+
83+
const postProcess = async (tempPath, destinationPath) => {
84+
const unpackedSourcePath = join(tempPath, "zip");
85+
const packedSourcePath = join(tempPath, "TestingLibraryDevTools.zip");
86+
const packedDestPath = join(destinationPath, "TestingLibraryDevTools.zip");
87+
const unpackedDestPath = join(destinationPath, "unpacked");
88+
89+
await move(unpackedSourcePath, unpackedDestPath); // Copy built files to destination
90+
await move(packedSourcePath, packedDestPath); // Copy built files to destination
91+
await remove(tempPath); // Clean up temp directory and files
92+
};
93+
94+
const main = async (buildId) => {
95+
const root = join(__dirname, buildId);
96+
const manifestPath = join(root, "manifest.json");
97+
const destinationPath = join(root, "build");
98+
99+
try {
100+
const tempPath = join(__dirname, "build", buildId);
101+
await preProcess(destinationPath, tempPath);
102+
await build(tempPath, manifestPath);
103+
104+
const builtUnpackedPath = join(destinationPath, "unpacked");
105+
await postProcess(tempPath, destinationPath);
106+
107+
return builtUnpackedPath;
108+
} catch (error) {
109+
// eslint-disable-next-line no-console
110+
console.error(error);
111+
// eslint-disable-next-line no-process-exit
112+
process.exit(1);
113+
}
114+
115+
return null;
116+
};
117+
118+
module.exports = main;

‎chrome/build.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
3+
/* eslint-disable no-process-exit */
4+
/* eslint-disable no-console */
5+
const { execSync } = require("child_process");
6+
const { existsSync } = require("fs");
7+
const { isAbsolute, join, relative } = require("path");
8+
const chalk = require("chalk");
9+
const { argv } = require("yargs");
10+
const build = require("../build");
11+
12+
const main = async () => {
13+
const { crx, keyPath } = argv;
14+
15+
if (crx) {
16+
if (!keyPath || !existsSync(keyPath)) {
17+
console.error("Must specify a key file (.pem) to build CRX");
18+
process.exit(1);
19+
}
20+
}
21+
22+
await build("chrome");
23+
24+
if (crx) {
25+
const cwd = join(__dirname, "build");
26+
27+
let safeKeyPath = keyPath;
28+
if (!isAbsolute(keyPath)) {
29+
safeKeyPath = join(relative(cwd, process.cwd()), keyPath);
30+
}
31+
32+
const crxPath = join(
33+
__dirname,
34+
"..",
35+
"..",
36+
"..",
37+
"node_modules",
38+
".bin",
39+
"crx"
40+
);
41+
42+
execSync(
43+
`${crxPath} pack ./unpacked -o TestingLibraryDevTools.crx -p ${safeKeyPath}`,
44+
{
45+
cwd,
46+
}
47+
);
48+
}
49+
50+
console.log(chalk.green("\nThe Chrome extension has been built!"));
51+
console.log(chalk.green("You can test this build by running:"));
52+
console.log(chalk.gray("\n# From the root directory:"));
53+
console.log("yarn run test:chrome");
54+
};
55+
56+
main();

‎manifest.json ‎chrome/manifest.json

+4-17
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,27 @@
33
"version": "0.0.1",
44
"manifest_version": 2,
55
"description": "Find the suggested query to use in your tests",
6-
"homepage_url": "https://testing-library.com/docs/guide-which-query",
6+
"homepage_url": "https://github.com/testing-library/which-query",
77
"icons": {
88
"16": "icons/icon16.png",
99
"48": "icons/icon48.png",
1010
"128": "icons/icon128.png"
1111
},
12-
"default_locale": "en",
1312
"background": {
1413
"scripts": ["dist/background.js"],
1514
"persistent": false
1615
},
17-
"web_accessible_resources": [
18-
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js",
19-
"src/globals.js"
20-
],
16+
"web_accessible_resources": ["dist/globals.js"],
2117
"content_scripts": [
2218
{
2319
"matches": ["<all_urls>"],
24-
"js": [
25-
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js",
26-
"dist/contentScript.js"
27-
]
20+
"js": ["dist/contentScript.js"]
2821
}
2922
],
3023
"browser_action": {
3124
"default_icon": "icons/icon48.png"
3225
},
3326
"content_security_policy": "script-src 'self' 'sha256-nP0EI9B9ad8IoFUti2q7EQBabcE5MS5v0nkvRfUbYnM='; object-src 'self'",
3427
"devtools_page": "pages/devtools.html",
35-
"permissions": [
36-
"activeTab",
37-
"contextMenus",
38-
"clipboardWrite",
39-
"clipboardRead",
40-
"notifications"
41-
]
28+
"permissions": ["activeTab", "contextMenus", "clipboardWrite"]
4229
}

‎git.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { execSync } = require("child_process");
2+
const { readFileSync } = require("fs");
3+
const { resolve } = require("path");
4+
5+
const GITHUB_URL = "https://github.com/facebook/react";
6+
7+
function getGitCommit() {
8+
try {
9+
return execSync("git show -s --format=%h").toString().trim();
10+
} catch (error) {
11+
// Mozilla runs this command from a git archive.
12+
// In that context, there is no Git revision.
13+
return null;
14+
}
15+
}
16+
17+
function getVersionString() {
18+
const packageVersion = JSON.parse(
19+
readFileSync(
20+
resolve(__dirname, "..", "react-devtools-core", "./package.json")
21+
)
22+
).version;
23+
24+
const commit = getGitCommit();
25+
26+
return `${packageVersion}-${commit}`;
27+
}
28+
29+
module.exports = {
30+
GITHUB_URL,
31+
getGitCommit,
32+
getVersionString,
33+
};

‎package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
],
2424
"scripts": {
2525
"build": "webpack",
26+
"build:chrome:crx": "cross-env NODE_ENV=production node ./chrome/build --crx",
27+
"build:chrome:dev": "cross-env NODE_ENV=development node ./chrome/build",
2628
"lint": "kcd-scripts lint",
2729
"setup": "npm install && npm run validate -s",
2830
"test": "kcd-scripts test",
@@ -38,14 +40,19 @@
3840
},
3941
"devDependencies": {
4042
"all-contributors-cli": "^6.14.2",
43+
"archiver": "^4.0.1",
44+
"chalk": "^4.1.0",
45+
"fs-extra": "^9.0.1",
4146
"kcd-scripts": "^6.0.0",
4247
"rollup": "^2.12.0",
4348
"webpack": "^4.43.0",
44-
"webpack-cli": "^3.3.11"
49+
"webpack-cli": "^3.3.11",
50+
"yargs": "^15.3.1"
4551
},
4652
"eslintIgnore": [
4753
"node_modules",
4854
"coverage",
49-
"dist"
55+
"dist",
56+
"lib"
5057
]
5158
}

‎src/contentScript.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,4 @@ function injectScript(scriptPath) {
107107
});
108108
}
109109

110-
injectScript(
111-
"node_modules/@testing-library/dom/dist/@testing-library/dom.umd.min.js"
112-
).then(() => {
113-
injectScript("src/globals.js");
114-
});
110+
injectScript("dist/globals.js");

‎src/elementsPanel.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Bridge = require("crx-bridge").default;
22
const prettier = require("prettier/standalone");
33
const babelParser = require("prettier/parser-babel");
44
const hljs = require("../lib/highlight.pack");
5+
56
Bridge.onMessage(
67
"show-suggestion",
78
({ data: { suggestedQuery, exactIndex, length } }) => {

‎src/globals.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
const { screen, fireEvent } = require("@testing-library/dom");
12
const Screen = Object.getPrototypeOf(window.screen);
23

3-
for (const func of Object.keys(window.TestingLibraryDom.screen)) {
4-
Screen[func] = window.TestingLibraryDom.screen[func];
4+
for (const func of Object.keys(screen)) {
5+
Screen[func] = screen[func];
56
}
67

7-
window.fireEvent = window.TestingLibraryDom.fireEvent;
8+
window.fireEvent = fireEvent;

‎webpack.config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ module.exports = {
66
contentScript: "./src/contentScript.js",
77
devtools: "./src/devtools.js",
88
elementsPanel: "./src/elementsPanel.js",
9+
globals: "./src/globals.js",
910
},
1011
output: {
1112
path: path.resolve(__dirname, "dist"),
1213
filename: "[name].js",
1314
},
14-
mode: "development",
15+
mode: process.env.NODE_ENV === "development" ? "development" : "production",
1516
devtool: "cheap-module-source-map",
1617
};

0 commit comments

Comments
 (0)
This repository has been archived.