Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 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
2 changes: 2 additions & 0 deletions .changeset/gold-cherries-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
28 changes: 28 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* This is a very stripped-down eslintrc, because @khanacademy/eslint-config
* assumes you're using react & flow (and requires a lot of dependencies along
* with it).
*/
const khan = require("@khanacademy/eslint-config");

// Only keep the rules that have no prefix (so are included in eslint core)
// and the prettier rule.
Comment on lines +8 to +9

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look at khan/eslint-config-khan and see that everything's listed as a peer dep. I wonder if we should change that.

const basicRules = {};
Object.keys(khan.rules).forEach((key) => {
if (!key.includes("/") || key.startsWith("prettier/")) {
basicRules[key] = khan.rules[key];
}
});

module.exports = {
extends: ["eslint:recommended", "prettier"],
plugins: ["prettier"],
rules: basicRules,
env: {
node: true,
},
parserOptions: {
sourceType: "module",
ecmaVersion: 2022,
},
};
14 changes: 14 additions & 0 deletions .github/workflows/node-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Node CI

on:
- pull_request

jobs:
check:
name: Lints
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: Khan/actions@check-for-changeset-v0.0.0
- uses: Khan/actions@shared-node-cache-v0.0.0
- run: yarn lint
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Release

on:
push:
branches:
- main

jobs:
release:
name: Release
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
node-version: [16.x]
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Publish as git tags
run: node utils/run-publish.mjs
1 change: 1 addition & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("@khanacademy/eslint-config/.prettierrc.js");
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
"workspaces": [
"actions/*"
],
"scripts": {
"lint": "eslint --ext .js,.mjs .",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize you pass a comma separated list to --ext, cool!

"fix": "yarn lint --fix"
},
"devDependencies": {
"@changesets/cli": "^2.22.0"
"@changesets/cli": "^2.22.0",
"eslint": "^8.15.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"@khanacademy/eslint-config": "^0.1.0",
"prettier": "^2.6.2"
}
}
60 changes: 60 additions & 0 deletions utils/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Building a package currently only consists of processing
* the action.yml to handle any references to other actions in
* this repo.
*/
import fs from "fs";
import path from "path";

function copyDir(src, dest) {
const entries = fs.readdirSync(src, {withFileTypes: true});
fs.mkdirSync(dest, {recursive: true});

for (const entry of entries) {
const srcPath = path.join(src, entry.name);
const destPath = path.join(dest, entry.name);

if (entry.isDirectory()) {
copyDir(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
}
}

export const processActionYml = (
name,
packageJsons,
actionYml,
monorepoName,
) => {
const replacements = [];
Object.keys(packageJsons[name].dependencies).forEach((depName) => {
replacements.push({
from: new RegExp(`\\buses: ${depName}\\b`, "g"),
to: `uses: ${monorepoName}#${depName}-v${packageJsons[depName].version}`,
});
});
replacements.forEach(({from, to}) => {
actionYml = actionYml.replace(from, to);
});

return actionYml;
};

export const buildPackage = (name, packageJsons, monorepoName) => {
const dist = `actions/${name}/dist`;
if (fs.existsSync(dist)) {
fs.rmSync(dist, {recursive: true});
}
copyDir(`actions/${name}`, dist);
if (packageJsons[name].dependencies) {
const yml = `actions/${name}/dist/action.yml`;
const actionYml = fs.readFileSync(yml, "utf8");
fs.writeFileSync(
yml,
processActionYml(name, packageJsons, actionYml, monorepoName),
);
}
return dist;
};
63 changes: 63 additions & 0 deletions utils/publish.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* This file determines which actions need to be published,
* "builds" them, and "publishes" them as git tags on the current repo.
* Building currently consists of checking the `action.yml`
* for references to other actions in this repo and replacing them
* with the appropriate pinned references.
*/
import fs from "fs";
import {execSync} from "child_process";
import {buildPackage} from "./build.mjs";

export const checkTag = (tag) => {
try {
execSync(`git show-ref --tags ${tag}`);
} catch (err) {
return false;
}
return true;
};

export const publishDirectoryAsTag = (distPath, origin, tag, dryRun) => {
const cmds = [
`git init .`,
`git add .`,
`git commit -m publish`,
`git remote add origin ${origin}`,
`git tag ${tag}`,
];
if (!dryRun) {
cmds.push(`git push origin ${tag}`);
}
cmds.forEach((cmd) => {
execSync(cmd, {cwd: distPath});
});
};

export const collectPackageJsons = (packageNames) => {
const packageJsons = {};
packageNames.forEach((name) => {
const pkg = JSON.parse(
fs.readFileSync(`actions/${name}/package.json`, "utf8"),
);
packageJsons[pkg.name] = pkg;
});
return packageJsons;
};

export const publishAsNeeded = (packageNames, dryRun = false) => {
execSync(`git fetch --tags`);
const origin = execSync(`git remote get-url origin`, {
encoding: "utf8",
}).trim();
const packageJsons = collectPackageJsons(packageNames);

packageNames.forEach((name) => {
const version = packageJsons[name].version;
const tag = `${name}-v${version}`;
if (!checkTag(tag)) {
const distPath = buildPackage(name, packageJsons, `Khan/actions`);
publishDirectoryAsTag(distPath, origin, tag, dryRun);
}
});
};
11 changes: 11 additions & 0 deletions utils/run-publish.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* This script runs whenever a change lands to the 'main' branch,
* publishing any new versions of actions that are needed.
*/
import fs from "fs";
import {publishAsNeeded} from "./publish.mjs";

const [_, __, ...args] = process.argv;
const dryRun = args.includes("--dry-run");
const packageNames = fs.readdirSync("actions");
publishAsNeeded(packageNames, dryRun);
Loading