Move all zx scripts to typescript

This commit is contained in:
Aria Minaei 2023-08-06 10:15:37 +02:00
parent d228b7635c
commit 4fefee863e
9 changed files with 94 additions and 368 deletions

View file

@ -1,3 +1,4 @@
import {$} from '@cspotcode/zx'
/**
* Builds all the packages for production
*/
@ -10,7 +11,7 @@ const packagesToBuild = [
'@theatre/r3f',
'theatric',
]
;(async function () {
async function build() {
// better quote function from https://github.com/google/zx/pull/167
$.quote = function quote(arg) {
if (/^[a-z0-9/_.-]+$/i.test(arg)) {
@ -37,4 +38,6 @@ const packagesToBuild = [
(workspace) => $`yarn workspace ${workspace} run build`,
),
])
})()
}
void build()

View file

@ -1,3 +1,5 @@
import {$} from '@cspotcode/zx'
/**
* cleans the build artifacts of all packages
*/
@ -11,7 +13,7 @@ const packages = [
'theatric',
]
;(async function () {
async function clean() {
// better quote function from https://github.com/google/zx/pull/167
$.quote = function quote(arg) {
if (/^[a-z0-9/_.-]+$/i.test(arg)) {
@ -35,4 +37,6 @@ const packages = [
await Promise.all([
...packages.map((workspace) => $`yarn workspace ${workspace} run clean`),
])
})()
}
void clean()

View file

@ -2,9 +2,10 @@
* This script publishes the insider packages from the CI. You can't run it locally unless you have a a valid npm access token and you store its value in the `NPM_TOKEN` environmental variable.
*/
import os from 'os'
import path from 'path'
import * as os from 'os'
import * as path from 'path'
import * as core from '@actions/core'
import {$} from '@cspotcode/zx'
const packagesToPublish = [
'@theatre/core',
@ -19,7 +20,7 @@ const packagesToPublish = [
/**
* Receives a version number and returns it without the tags, if there are any
*
* @param {string} version - Version number
* @param version - Version number
* @returns Version number without the tags
*
* @example
@ -30,7 +31,7 @@ const packagesToPublish = [
* stripTag(version_1) === stripTag(version_2) === '0.4.8' // returns `true`
* ```
*/
function stripTag(version) {
function stripTag(version: string) {
const regExp = /^[0-9]+\.[0-9]+\.[0-9]+/g
const matches = version.match(regExp)
if (!matches) {
@ -43,10 +44,10 @@ function stripTag(version) {
/**
* Creates a version number like `0.4.8-insiders.ec175817`
*
* @param {string} packageName - Name of the package
* @param {string} commitHash - A commit hash
* @param packageName - Name of the package
* @param commitHash - A commit hash
*/
function getNewVersionName(packageName, commitHash) {
function getNewVersionName(packageName: string, commitHash: string) {
// The `r3f` package has its own release schedule, so its version numbers
// are almost always different from the rest of the packages.
const pathToPackageJson =
@ -65,16 +66,15 @@ function getNewVersionName(packageName, commitHash) {
/**
* Assigns the latest version names ({@link getNewVersionName}) to the packages' `package.json`s
*
* @param {{name: string, location: string}[]} workspacesListObjects - An Array of objects containing information about the workspaces
* @param {string} latestCommitHash - Hash of the latest commit
* @returns {Promise<Record<string, string>>} - A record of {[packageId]: assignedVersion}
* @param workspacesListObjects - An Array of objects containing information about the workspaces
* @param latestCommitHash - Hash of the latest commit
* @returns - A record of `{[packageId]: assignedVersion}`
*/
async function writeVersionsToPackageJSONs(
workspacesListObjects,
latestCommitHash,
) {
/** @type {Record<string, string>} */
const assignedVersionByPackageName = {}
workspacesListObjects: {name: string; location: string}[],
latestCommitHash: string,
): Promise<Record<string, string>> {
const assignedVersionByPackageName: Record<string, string> = {}
for (const workspaceData of workspacesListObjects) {
const pathToPackage = path.resolve(
__dirname,
@ -124,7 +124,7 @@ async function writeVersionsToPackageJSONs(
return assignedVersionByPackageName
}
;(async function () {
async function prerelease() {
// @ts-ignore ignore
process.env.THEATRE_IS_PUBLISHING = true
// In the CI `git log -1` points to a fake merge commit,
@ -180,4 +180,6 @@ async function writeVersionsToPackageJSONs(
await $`echo ${`Published ${packageName}@${assignedVersionByPackageName[packageName]}`}`
}
}
})()
}
void prerelease()

View file

@ -6,15 +6,15 @@
* $ yarn run release 0.4.2
* ```
*/
import path from 'path'
import * as path from 'path'
import {readFileSync, writeFileSync} from 'fs'
import {$} from '@cspotcode/zx'
/**
* This script publishes all packages to npm.
*
* It assigns the same version number to all packages (like lerna's fixed mode).
**/
// It's written in .mjs because I kept running into issues with zx+typescript
const packagesToBuild = [
'theatre',
'@theatre/dataverse',
@ -49,32 +49,32 @@ const packagesWhoseVersionsShouldBump = [
'packages/theatric',
]
;(async function () {
// our packages will check for this env variable to make sure their
// prepublish script is only called from the `$ cd /path/to/monorepo; yarn run release`
// @ts-ignore ignore
process.env.THEATRE_IS_PUBLISHING = true
// our packages will check for this env variable to make sure their
// prepublish script is only called from the `$ cd /path/to/monorepo; yarn run release`
// @ts-ignore ignore
process.env.THEATRE_IS_PUBLISHING = true
// better quote function from https://github.com/google/zx/pull/167
$.quote = function quote(arg) {
if (/^[a-z0-9/_.-]+$/i.test(arg)) {
return arg
}
return (
`$'` +
arg
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/\f/g, '\\f')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t')
.replace(/\v/g, '\\v')
.replace(/\0/g, '\\0') +
`'`
)
// better quote function from https://github.com/google/zx/pull/167
$.quote = function quote(arg) {
if (/^[a-z0-9/_.-]+$/i.test(arg)) {
return arg
}
return (
`$'` +
arg
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
.replace(/\f/g, '\\f')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t')
.replace(/\v/g, '\\v')
.replace(/\0/g, '\\0') +
`'`
)
}
async function release() {
$.verbose = false
const gitTags = (await $`git tag --list`).toString().split('\n')
@ -188,10 +188,11 @@ const packagesWhoseVersionsShouldBump = [
$`yarn workspace ${workspace} npm publish --access public --tag ${npmTag}`,
),
)
})()
}
/** @param {string} monorepoVersion */
async function writeVersionsToPackageJSONs(monorepoVersion) {
void release()
async function writeVersionsToPackageJSONs(monorepoVersion: string) {
for (const packagePathRelativeFromRoot of packagesWhoseVersionsShouldBump) {
const pathToPackage = path.resolve(
__dirname,

View file

@ -1,11 +1,11 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"resolveJsonModule": true,
"types": ["zx", "node"]
"types": ["node"],
"composite": true,
"target": "es6"
},
"include": ["*.mjs", "**/*.mjs"]
"include": ["*.ts", "**/*.ts"]
}