#!/usr/bin/env bun import * as BunRuntime from '@effect/platform-bun/BunRuntime' import * as BunServices from '@effect/platform-bun/BunServices' import * as Cause from 'effect/Cause' import * as Prompt from 'effect/cli/Prompt' import * as Effect from 'effect/Effect' import * as FileSystem from 'effect/FileSystem' import * as Path from 'effect/Path' import * as ChildProcess from 'effect/process/ChildProcess' import * as ChildProcessSpawner from 'effect/process/ChildProcessSpawner' import * as Record from 'effect/Record' import type { SubPackageJson } from './fix_monorepo.ts' import { packagesDirPath, projectRootAbsolutePath } from './lib/paths.ts' export const vscodeConfig = Effect.fn('vscodeConfig')(function* ( _packagePath: string, ) { const _path = yield* Path.Path // path.relative( // packagePath, // path.join(packagesDirPath, './node_modules/typescript'), // ) return { // TODO: make them synced with recomended settings? 'git.openRepositoryInParentFolders': 'always', 'js/ts.tsdk.path': './node_modules/typescript', 'js/ts.experimental.useTsgo': true, 'js/ts.tsdk.promptToUseWorkspaceVersion': true, 'editor.codeActionsOnSave': { 'source.organizeImports.biome': 'explicit', 'source.fixAll.biome': 'explicit', }, 'js/ts.preferences.importModuleSpecifier': 'project-relative', 'js/ts.preferences.importModuleSpecifierEnding': 'js', 'js/ts.preferences.preferTypeOnlyAutoImports': true, 'prettier.jsxSingleQuote': true, 'prettier.singleQuote': true, } }) export const biomeDefaultConfig = { // TODO: use the schema url from the main biome.jsonc $schema: 'https://biomejs.dev/schemas/2.5.15/schema.json', extends: '//', } export const githubUser = 'nikelborm' export const repo = 'effect-garden' export const email = 'evadev@duck.com' export const userProtolessLink = `github.com/${githubUser}` export const repoProtolessLink = `${userProtolessLink}/${repo}` export const httpsRepoLink = `https://${repoProtolessLink}` export const httpsUserLink = `https://${userProtolessLink}` export const issuesLink = `${httpsRepoLink}/issues` export const gitSshUrl = `git+ssh://git@${repoProtolessLink}.git` export const author = { name: githubUser, email, url: httpsUserLink } as const const packageJson = (config: { name: string; description: string }) => ({ name: config.name, version: '0.1.0', type: 'module', description: config.description, license: 'MIT', scripts: { build: 'tsc', prepack: 'rm -rf dist dist-types && ./node_modules/.bin/tsc', dev: 'tsc --watch --preserveWatchOutput false', }, files: [ 'dist-types', 'dist', 'index.ts', 'package.json', 'src', '!**/*.spec.*', '!**/*.tsbuildinfo', '!**/scratchpad.*', ], peerDependencies: { effect: 'catalog:', }, // TODO: Ask for package name and folder name separately homepage: `${httpsRepoLink}/tree/main/packages/${config.name}#readme`, devDependencies: { '@biomejs/biome': 'catalog:', '@evadev/tsconfig': 'workspace:^', '@effect/tsgo': 'catalog:', oxlint: 'catalog:', 'ts-namespace-import': 'catalog:', 'ts-patch': 'catalog:', typescript: 'catalog:', }, private: false, publishConfig: { access: 'public', provenance: false }, repository: { type: 'git', url: gitSshUrl, directory: `packages/${config.name}`, }, bugs: { url: issuesLink, email }, main: './dist/index.js', module: './dist/index.js', types: './dist-types/index.d.ts', exports: { '.': { types: './dist-types/index.d.ts', default: './dist/index.js', }, './*.js': { types: './dist-types/src/*.d.ts', default: './dist/src/*.js', }, './*.ts': './src/*.ts', './*': { types: './dist-types/src/*.d.ts', default: './dist/src/*.js', }, './internal/*': null, './package.json': './package.json', }, author: author, contributors: [author], maintainers: [author], }) as const satisfies SubPackageJson const tsconfigJson = { extends: '@evadev/tsconfig', compilerOptions: { declarationDir: './dist-types', outDir: './dist', rootDir: './', }, include: ['**/*.ts'], exclude: [ '.github', '.stryker-tmp', '.turbo', '.vscode', 'build', 'dist-types', 'dist', 'gh-page', 'node_modules', 'out', 'reports', 'tmp', 'vite.config.ts', ], } const emptyInternalTsNamespaceFile = `/** biome-ignore-all lint/style/useShorthandFunctionType: It's a nice way to * preserve JSDoc comments attached to the function signature */ import * as Effect from 'effect/Effect' ` const program = Effect.gen(function* () { const { namespaces, ...config } = yield* Prompt.all({ name: Prompt.String({ message: `Enter package name (if you want, add 'effect-' prefix):`, validate: value => value.length <= 3 ? Effect.fail('Name is too short. At least 3 characters') : Effect.succeed(value), }), description: Prompt.String({ message: `Enter single-line short package description for readme and package.json:`, validate: value => value.length <= 3 ? Effect.fail('Name is too short. At least 3 characters') : Effect.succeed(value), }), namespaces: Prompt.List({ delimiter: ' ', message: 'Enter a list of namespaces to create empty files for, delimited by space', validate: value => value.length <= 1 ? Effect.fail('Name is too short. At least 3 characters') : Effect.succeed(value), }), }).pipe(Prompt.run) const fs = yield* FileSystem.FileSystem const path = yield* Path.Path const packagePath = path.join(packagesDirPath, config.name) const vscodeDirPath = path.join(packagePath, '.vscode') const srcDirPath = path.join(packagePath, 'src') const internalDirPath = path.join(srcDirPath, 'internal') yield* fs.makeDirectory(vscodeDirPath, { recursive: true }) yield* fs.makeDirectory(internalDirPath, { recursive: true }) const toJSON = (e: any) => JSON.stringify(e, null, 2) + '\n' const map = { [path.join(vscodeDirPath, 'settings.json')]: toJSON( yield* vscodeConfig(packagePath), ), [path.join(packagePath, 'biome.jsonc')]: toJSON(biomeDefaultConfig), [path.join(packagePath, 'mise.toml')]: `[env._]\npath = ["./node_modules/.bin"]\n`, [path.join(packagePath, 'package.json')]: toJSON(packageJson(config)), [path.join(packagePath, 'tsconfig.json')]: toJSON(tsconfigJson), [path.join(packagePath, 'README.md')]: '# ' + config.name + '\n\n' + config.description + '\n', [path.join(packagePath, 'index.ts')]: `export * from './src/index.ts'\n`, [path.join(srcDirPath, 'index.ts')]: namespaces .map(namespace => `export * as ${namespace} from './${namespace}.ts'\n`) .join(''), ...Object.fromEntries( namespaces.flatMap(namespace => [ [ path.join(srcDirPath, namespace + '.ts'), `export {} from './internal/${namespace}.ts'\n`, ], [ path.join(internalDirPath, namespace + '.ts'), emptyInternalTsNamespaceFile, ], ]), ), } yield* Effect.forEach(Record.toEntries(map), ([path, content]) => fs.writeFileString(path, content), ) const spawner = yield* ChildProcessSpawner.ChildProcessSpawner yield* spawner.exitCode( ChildProcess.make('bun', ['add', config.name + '@workspace:^'], { cwd: projectRootAbsolutePath, stderr: 'inherit', stdout: 'inherit', }), ) yield* spawner.exitCode( ChildProcess.make('bun', ['install'], { cwd: packagePath, stderr: 'inherit', stdout: 'inherit', }), ) }).pipe( Effect.provide(BunServices.layer), Effect.withSpan(import.meta.file), Effect.sandbox, Effect.catch(e => { console.error(Cause.pretty(e)) return Effect.fail(e) }), ) if (import.meta.main) BunRuntime.runMain(program)