website/scripts/add_package.ts

79 lines
1.9 KiB
TypeScript
Raw Normal View History

import { ensureDir } from '$std/fs/mod.ts'
import { join } from '$std/path/join.ts'
import denoJson from '../deno.json' with { type: 'json' }
// Get package from cli
const baseRepo = 'https://git.cohabit.fr/'
const packageRepoAndTag = Deno.args[0]
const match = packageRepoAndTag.match(/(?<name>@\S+\/\S+)@(?<tag>\d\.\d\.\d.*)/)
// Parse name and version tag
if (match === null || match.groups === undefined) {
throw new SyntaxError('usage: deno task $task_name @scope/name@x.x.x')
}
const { name, tag } = match.groups
const repoUrl = new URL(`${name.slice(1)}.git`, baseRepo)
// Setup destination and remove old versions
const fsName = name.replace('/', '__')
const packageBase = join(Deno.cwd(), 'packages')
const destinationBase = join(packageBase, fsName)
const destinationCurrent = `${destinationBase}@${tag}`
// Create base dir if needed
await ensureDir(packageBase)
// Remove old versions
for await (const entry of Deno.readDir(packageBase)) {
if (entry.isDirectory && entry.name.startsWith(fsName)) {
Deno.remove(join(packageBase, entry.name), {
recursive: true,
})
}
}
// Clone repo @ tag
const git = new Deno.Command('git', {
args: [
'clone',
repoUrl.href,
'--branch',
tag,
'--single-branch',
'--depth',
'1',
destinationCurrent,
],
stderr: 'inherit',
stdout: 'inherit',
})
await git.output()
// Get workspaces
// @ts-ignore maybe undefined
const workspaces: string[] = denoJson['workspaces'] ?? []
// Update deno.json
const newDenoJson = {
...denoJson,
imports: {
...denoJson.imports,
[`${name}/`]: `./packages/${fsName}@${tag}/`,
},
workspaces: [
...workspaces.filter((workspace) =>
workspace.startsWith(`packages/${fsName}`) === false
),
`packages/${fsName}@${tag}`,
],
}
// Update deno.json file
await Deno.writeTextFile('./deno.json', JSON.stringify(newDenoJson))
const denoFmt = new Deno.Command('deno', {
args: ['fmt', 'deno.json'],
stderr: 'inherit',
stdout: 'inherit',
})
await denoFmt.output()