Update build and tests

This commit is contained in:
lowlighter
2020-11-06 19:59:44 +01:00
parent 21f2fa7bec
commit 8d1b29b711
6 changed files with 114 additions and 43 deletions

5
package-lock.json generated
View File

@@ -405,6 +405,11 @@
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz",
"integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU="
}, },
"colors": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
},
"compressible": { "compressible": {
"version": "2.0.18", "version": "2.0.18",
"resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz", "resolved": "https://registry.npmjs.org/compressible/-/compressible-2.0.18.tgz",

View File

@@ -25,6 +25,7 @@
"@octokit/graphql": "^4.5.7", "@octokit/graphql": "^4.5.7",
"@octokit/rest": "^18.0.9", "@octokit/rest": "^18.0.9",
"axios": "^0.21.0", "axios": "^0.21.0",
"colors": "^1.4.0",
"compression": "^1.7.4", "compression": "^1.7.4",
"ejs": "^3.1.5", "ejs": "^3.1.5",
"express": "^4.17.1", "express": "^4.17.1",

View File

@@ -1,3 +1,5 @@
/* This file is generated automatically with "npm run build" */
//Imports //Imports
import followup from "./followup/index.mjs" import followup from "./followup/index.mjs"
import habits from "./habits/index.mjs" import habits from "./habits/index.mjs"

View File

@@ -1,3 +1,5 @@
/* This file is generated automatically with "npm run build" */
//Imports //Imports
import classic from "./classic/template.mjs" import classic from "./classic/template.mjs"
import terminal from "./terminal/template.mjs" import terminal from "./terminal/template.mjs"

View File

@@ -1,11 +1,8 @@
//Imports //Imports
import path from "path"
import fs from "fs"
import build from "../utils/build.mjs" import build from "../utils/build.mjs"
import url from "url" import colors from "colors"
//Initialization //Initialization
const __dirname = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "..", "action")
process.on("unhandledRejection", error => { throw error }) process.on("unhandledRejection", error => { throw error })
/** Test function */ /** Test function */
@@ -16,17 +13,14 @@
/** Build test */ /** Build test */
test.build = async function () { test.build = async function () {
//Ensure that action has been rebuild //Ensure that code has been rebuild
console.log("### Checking that code has been rebuild") console.log("TEST : build".cyan)
const action = `${await fs.promises.readFile(`${__dirname}/dist/index.js`)}` await build({actions:["check"]})
const code = await build()
if (action !== code)
throw new Error(`GitHub Action has not been rebuild. Run "npm run build" to solve this issue`)
} }
//Main //Main
if (/metrics.mjs/.test(process.argv[1])) { if (/metrics.mjs/.test(process.argv[1])) {
//Test //Test
await test() await test()
console.log("Test success !") console.log("Test success !".green)
} }

View File

@@ -3,49 +3,116 @@
import path from "path" import path from "path"
import url from "url" import url from "url"
import ncc from "@vercel/ncc" import ncc from "@vercel/ncc"
import colors from "colors"
//Initialization //Initialization
const __dirname = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "..", "action") const __dirname = path.join(path.dirname(url.fileURLToPath(import.meta.url)), "..")
const __action = path.join(__dirname, "action")
const __src = path.join(__dirname, "src")
const __plugins = path.join(__src, "plugins")
const __templates = path.join(__src, "templates")
process.on("unhandledRejection", error => { throw error }) process.on("unhandledRejection", error => { throw error })
/** Build function */ /** Build function */
export default async function build() { export default async function build({actions = ["build"]} = {}) {
//Build code //Initialization
let {code} = await ncc(`${__dirname}/index.mjs`, { const errors = []
//Indexes
for (const {name, source, entry} of [{name:"plugins", source:__plugins, entry:"index.mjs"}, {name:"templates", source:__templates, entry:"template.mjs"}]) {
//Build
const files = (await fs.promises.readdir(source)).filter(name => !/.*[.]mjs$/.test(name)).sort()
const code = [
`/* This file is generated automatically with "npm run build" */`,
``,
`//Imports`,
...files.map(name => ` import ${name} from "./${name}/${entry}"`),
``,
`//Exports`,
` export default {`,
...files.map(name => ` ${name},`),
` }`
].join("\n")
console.log(`Generated index for ${name}`.grey)
//Save build
if (actions.includes("build")) {
fs.promises.writeFile(path.join(source, "index.mjs"), code)
console.log(`Generated index for ${name} saved to ${path.join(source, "index.mjs")}`.green)
}
//Check build
if (actions.includes("check")) {
const status = `${await fs.promises.readFile(path.join(source, "index.mjs"))}` === code
if (status)
console.log(`Index ${name} is up-to-date`.grey)
else {
console.log(`Index ${name} is outdated`.red)
errors.push(`Index ${name} is outdated, run "npm run build" to fix it`)
}
}
}
//Action
{
//Build
let {code} = await ncc(`${__action}/index.mjs`, {
minify:true, minify:true,
sourceMap:false, sourceMap:false,
sourceMapRegister:false, sourceMapRegister:false,
}) })
console.log(`Generated action`.grey)
//Perform assets includes //Perform assets includes
const assets = {} const assets = {}
const templates = path.join(__dirname, "..", "src/templates") const templates = (await fs.promises.readdir(__templates)).filter(name => !/.*[.]mjs$/.test(name)).sort()
for (const name of await fs.promises.readdir(templates)) { for (const name of templates) {
if (/.*[.]mjs$/.test(name))
continue
console.log(`Including template ${name}`)
const files = [ const files = [
`${templates}/${name}/query.graphql`, `${__templates}/${name}/query.graphql`,
`${templates}/${name}/image.svg`, `${__templates}/${name}/image.svg`,
`${templates}/${name}/style.css`, `${__templates}/${name}/style.css`,
`${templates}/${name}/fonts.css`, `${__templates}/${name}/fonts.css`,
] ]
const [query, image, style, fonts] = await Promise.all(files.map(async file => `${await fs.promises.readFile(path.resolve(file))}`)) const [query, image, style, fonts] = await Promise.all(files.map(async file => `${await fs.promises.readFile(path.resolve(file))}`))
assets[name] = {query, image, style, fonts} assets[name] = {query, image, style, fonts}
console.log(`Prepared template ${name}`.grey)
} }
code = code.replace(/<#assets>/g, Buffer.from(JSON.stringify(assets)).toString("base64")) code = code.replace(/<#assets>/g, Buffer.from(JSON.stringify(assets)).toString("base64"))
console.log(`Included ${templates.length} templates to generated action`.grey)
//Perform version include //Perform version include
const version = JSON.parse(await fs.promises.readFile(path.join(__dirname, "..", "package.json"))).version const version = JSON.parse(await fs.promises.readFile(path.join(__dirname, "package.json"))).version
code = code.replace(/<#version>/g, version) code = code.replace(/<#version>/g, version)
console.log(`Included version number (${version}) to generated action`.grey)
//Code //Save build
return code if (actions.includes("build")) {
fs.promises.writeFile(path.join(__action, "dist/index.js"), code)
console.log(`Generated action saved to ${path.join(__action, "dist/index.js")}`.green)
}
//Check build
if (actions.includes("check")) {
const status = `${await fs.promises.readFile(path.join(__action, "dist/index.js"))}` === code
if (status)
console.log(`Action is up-to-date`.grey)
else {
console.log(`Action is outdated`.red)
errors.push(`Action is outdated, run "npm run build" to fix it`)
}
}
}
//
if (errors.length)
throw new Error(`${errors.length} errors occured :\n${errors.map(error => ` - ${error}`).join("\n")}`)
} }
//Main //Main
if (/build.mjs/.test(process.argv[1])) { if (/build.mjs/.test(process.argv[1])) {
//Save build //Build
await fs.promises.writeFile(`${__dirname}/dist/index.js`, await build()) await build()
console.log("Build success !") console.log("Build success !".green)
} }