Skip to content

Commit

Permalink
fix(deploy): update deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Oct 23, 2020
1 parent ed8b7eb commit cd8631b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 532 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ jobs:
- name: Install
run: npm ci

- name: Inject
run: node file-content-injector.js

- name: Test
if: ${{ github.ref == 'refs/heads/master' }}
run: npm test
Expand Down
12 changes: 12 additions & 0 deletions deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ server {
const DIST_REMOTE_FOLDER = join(REMOTE_ROOT, DIST_LOCAL_FOLDER);
const POLYFILL_LIB_LOCAL_FOLDER = "polyfill-lib";
const POLYFILL_LIB_REMOTE_FOLDER = join(REMOTE_ROOT, POLYFILL_LIB_LOCAL_FOLDER);
const FILE_CONTENT_INJECTOR_LOCAL_FILE_NAME = join(LOCAL_WRITE_ROOT, `file-content-injector.js`);
const FILE_CONTENT_INJECTOR_REMOTE_FILE_NAME = join(REMOTE_ROOT, `file-content-injector.js`);
const PACKAGE_LOCK_LOCAL_FILE_NAME = join(LOCAL_WRITE_ROOT, `package-lock.json`);
const PACKAGE_LOCK_REMOTE_FILE_NAME = join(REMOTE_ROOT, `package-lock.json`);
const PACKAGE_JSON_LOCAL_FILE_NAME = join(LOCAL_WRITE_ROOT, `package.json`);
Expand Down Expand Up @@ -250,6 +252,12 @@ server {
concurrency: 1
});

// Copy over the file-content-injector.js file
console.log(`Creating ${FILE_CONTENT_INJECTOR_REMOTE_FILE_NAME}`);
await ssh.putFile(FILE_CONTENT_INJECTOR_LOCAL_FILE_NAME, FILE_CONTENT_INJECTOR_REMOTE_FILE_NAME, sftp, {
concurrency: 1
});

// Copy over the built dist folder
console.log(`Creating ${DIST_REMOTE_FOLDER}`);
await ssh.putDirectory(DIST_LOCAL_FOLDER, DIST_REMOTE_FOLDER, {concurrency: 1, sftp, transferOptions: {concurrency: 1}});
Expand All @@ -262,6 +270,10 @@ server {
console.log(`Installing in ${REMOTE_ROOT}`);
await ssh.execCommand(`npm ci`, {cwd: REMOTE_ROOT});

// Inject
console.log(`Injecting file content replacements in ${REMOTE_ROOT}`);
await ssh.execCommand(`node file-content-injector.js`, {cwd: REMOTE_ROOT});

// Run
console.log(`Running`);
const pm2NeverRan = (await ssh.execCommand(`npx pm2 show ${APP_NAME}`, {cwd: REMOTE_ROOT})).stdout === "";
Expand Down
26 changes: 26 additions & 0 deletions file-content-injector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require("fs");

[
{
file: "node_modules/core-js/internals/whitespaces.js",
match: String.raw`module.exports = '\u0009\u000A\u000B\u000C\u000D\u0020\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u202F\u205F\u3000\u2028\u2029\uFEFF';`,
replacement: String.raw`module.exports = String.fromCharCode("0x0009") + String.fromCharCode("0x000A") + String.fromCharCode("0x000B") + String.fromCharCode("0x000C") + String.fromCharCode("0x000D") + String.fromCharCode("0x0020") + String.fromCharCode("0x00A0") + String.fromCharCode("0x1680") + String.fromCharCode("0x2000") + String.fromCharCode("0x2001") + String.fromCharCode("0x2002") + String.fromCharCode("0x2003") + String.fromCharCode("0x2004") + String.fromCharCode("0x2005") + String.fromCharCode("0x2006") + String.fromCharCode("0x2007") + String.fromCharCode("0x2008") + String.fromCharCode("0x2009") + String.fromCharCode("0x200A") + String.fromCharCode("0x202F") + String.fromCharCode("0x205F") + String.fromCharCode("0x3000") + String.fromCharCode("0x2028") + String.fromCharCode("0x2029") + String.fromCharCode("0xFEFF");`
}
].forEach(replace);

function replace({file, match, replacement}) {
if (!fs.existsSync(file)) {
throw new ReferenceError(`No such file: ${file}`);
}

const fileContents = fs.readFileSync(file, "utf8");
const matchPosition = fileContents.indexOf(match);
const replacementPosition = fileContents.indexOf(replacement);
if (matchPosition < 0 && replacementPosition < 0) {
throw new ReferenceError(`File: ${file} does not include: ${match}`);
} else if (matchPosition >= 0) {
const updatedFileContents = fileContents.slice(0, matchPosition) + replacement + fileContents.slice(matchPosition + match.length);
fs.writeFileSync(file, updatedFileContents);
console.log("Updated contents of file:", file);
}
}
5 changes: 1 addition & 4 deletions src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {join} from "path";
import {generateRandomHash} from "../util/hash-util/hash-util";
import {unlinkSync, writeFileSync} from "fs";
import {transform} from "@swc/core";
import {UnicodeEscapeRestorer} from "./swc/unicode-escape-restorer";

function stringifyPolyfillFeature(feature: IPolyfillFeature): string {
const metaEntries = Object.entries(feature.meta);
Expand Down Expand Up @@ -91,9 +90,7 @@ export async function build({paths, features, featuresRequested, userAgent, cont
filename: virtualOutputFileName,
jsc: {
target: ecmaVersion
},
// replace unicode characters with a builder pattern to fix issues in legacy browsers
plugin: m => new UnicodeEscapeRestorer().visitProgram(m)
}
}));
}

Expand Down
Loading

0 comments on commit cd8631b

Please sign in to comment.