Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Everything that must pass on pull requests and pushes to `dev`: the pull
# request title, conflict markers, linting (includes the build) with the
# tests of the release script and the validation of the citation metadata,
# tests of the scripts and the validation of the citation metadata,
# and tests on all supported Node.js versions, with coverage reported to
# Codecov. The gate `passed` ("CI passed") is the only check that branch
# rules need to require. No `paths:` filters, so that the gate always
Expand Down Expand Up @@ -93,8 +93,8 @@ jobs:
- name: Install dependencies
run: npm ci

- name: Test the release script
run: node --test scripts/release.test.js
- name: Test the scripts
run: node --test scripts/release.test.js scripts/utils.test.js

# In its own environment, as cffconvert pins an old jsonschema. 2.0.0
# is the latest stable release (not bumped by Dependabot; update
Expand Down
26 changes: 15 additions & 11 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { cwd } = require("process");
const fg = require("fast-glob");
const tsup = require("tsup");
const { program } = require("commander");
const { execSync } = require("child_process");
const { execFileSync } = require("child_process");
const fs = require("fs");
const { npmDir, normalizePath } = require("./utils.js");
const path = require("path");
Expand Down Expand Up @@ -74,15 +74,16 @@ nextTask("verifying dependencies", async () => {
});

nextTask("Generating CEM config", () => {
execSync(`node ${path.join(__dirname, "make-CEMconfig.js")}`, {
execFileSync(process.execPath, [path.join(__dirname, "make-CEMconfig.js")], {
stdio: "inherit",
});
});

nextTask("Generating component metadata", () => {
bundleDirectories.map((dir) =>
execSync(
`node ${path.join(__dirname, "make-metadata.js")} --outdir "${dir}"`,
execFileSync(
process.execPath,
[path.join(__dirname, "make-metadata.js"), "--outdir", dir],
{
stdio: "inherit",
}
Expand All @@ -91,20 +92,23 @@ nextTask("Generating component metadata", () => {
});

nextTask("Wrapping components for React", async () => {
execSync(
`node ${path.join(__dirname, "make-react.js")} -p "${
commanderOpts.prefix
}"`,
execFileSync(
process.execPath,
[path.join(__dirname, "make-react.js"), "-p", `${commanderOpts.prefix}`],
{
stdio: "inherit",
}
);
});

nextTask("Running the TypeScript compiler", () => {
execSync(`tsc --project ./tsconfig.prod.json --outdir "${npmDir}"`, {
stdio: "inherit",
});
execFileSync(
"tsc",
["--project", "./tsconfig.prod.json", "--outdir", npmDir],
{
stdio: "inherit",
}
);
});

// TODO
Expand Down
7 changes: 4 additions & 3 deletions scripts/make-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
const { program } = require("commander");
const options = program.option("-d, --outdir <string>").parse().opts();

const { execSync } = require("child_process");
const { execFileSync } = require("child_process");

execSync(
`custom-elements-manifest analyze --litelement --outdir "${options.outdir}"`,
execFileSync(
"custom-elements-manifest",
["analyze", "--litelement", "--outdir", options.outdir],
{
stdio: "inherit",
}
Expand Down
26 changes: 14 additions & 12 deletions scripts/tailwind/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { execSync, spawn } = require("child_process");
const { execFileSync, spawn } = require("child_process");
const glob = require("fast-glob");
const yargs = require("yargs");
const { escapeTemplateLiteral } = require("../utils.js");

try {
const argv = yargs(process.argv.slice(2)).argv;
Expand Down Expand Up @@ -105,9 +106,9 @@ try {
fs.watchFile(componentCssPath, { interval: 1000 }, () => {
try {
// When CSS file changes, update the Lit template
let cssContent = fs.readFileSync(componentCssPath, "utf8");
cssContent = cssContent.replaceAll("`", "\\`");
cssContent = cssContent.replaceAll("\\", "\\\\");
const cssContent = escapeTemplateLiteral(
fs.readFileSync(componentCssPath, "utf8")
);

const litTemplate = `
import { css } from "lit";
Expand All @@ -129,9 +130,9 @@ export const ComponentStyles = css\` ${cssContent} \`

// Initial file read once it exists
try {
let cssContent = fs.readFileSync(componentCssPath, "utf8");
cssContent = cssContent.replaceAll("`", "\\`");
cssContent = cssContent.replaceAll("\\", "\\\\");
const cssContent = escapeTemplateLiteral(
fs.readFileSync(componentCssPath, "utf8")
);

const litTemplate = `
import { css } from "lit";
Expand Down Expand Up @@ -163,17 +164,18 @@ export const ComponentStyles = css\` ${cssContent} \`
setTimeout(setupWatcher, 2000);
} else {
// In non-watch mode, process synchronously
execSync(
`npx @tailwindcss/cli -i ${configPath} -o ${componentCssPath}`,
execFileSync(
"npx",
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
["@tailwindcss/cli", "-i", configPath, "-o", componentCssPath],
{
stdio: "inherit",
}
);

// Convert to Lit template
let cssContent = fs.readFileSync(componentCssPath, "utf8");
cssContent = cssContent.replaceAll("`", "\\`");
cssContent = cssContent.replaceAll("\\", "\\\\");
const cssContent = escapeTemplateLiteral(
fs.readFileSync(componentCssPath, "utf8")
);

const litTemplate = `
import { css } from "lit";
Expand Down
11 changes: 11 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,22 @@ function normalizePath(filename) {
return filename.split(path.win32.sep).join(path.posix.sep);
}

// Escapes text for a template literal, so that it evaluates to the same text.
// Backslashes go first: escaping them last would also escape the backslashes
// that the other replacements add
function escapeTemplateLiteral(text) {
return text
.replaceAll("\\", "\\\\")
.replaceAll("`", "\\`")
.replaceAll("${", "\\${");
}

module.exports = {
componentsPrefix,
npmDir,
getAllComponents,
pascalCase,
reactDir: path.join(cwd(), ".", "src", "react"),
normalizePath,
escapeTemplateLiteral,
};
34 changes: 34 additions & 0 deletions scripts/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Tests for scripts/utils.js; run with `node --test scripts/utils.test.js`

const assert = require("assert");
const { test } = require("node:test");

const { escapeTemplateLiteral } = require("./utils.js");

// What a template literal with the escaped text in it evaluates to
const evaluate = (escaped) =>
// eslint-disable-next-line no-new-func
new Function(`return \`${escaped}\`;`)();

test("escaped text survives a template literal unchanged", () => {
[
"",
".a { color: red; }",
'.a::before { content: "\\201C"; }',
".a { content: '`'; }",
".a { content: '\\`'; }",
".a { content: '${b}'; }",
".a { content: '\\${b}'; }",
".a { content: '$'; } .b { content: '{'; }",
"\\\\",
"`${`\\",
].forEach((text) =>
assert.strictEqual(evaluate(escapeTemplateLiteral(text)), text)
);
});

test("backslashes are escaped before backticks", () => {
assert.strictEqual(escapeTemplateLiteral("`"), "\\`");
assert.strictEqual(escapeTemplateLiteral("\\"), "\\\\");
assert.strictEqual(escapeTemplateLiteral("\\`"), "\\\\\\`");
});
Loading