-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup build and hooks packages
- Loading branch information
Showing
15 changed files
with
992 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
node_modules | ||
*.tgz | ||
tsconfig.tsbuildinfo | ||
.parcel-cache | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import * as fs from 'fs'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import spawn from '@expo/spawn-async'; | ||
import esbuild from 'esbuild'; | ||
import Debug from 'debug'; | ||
import * as util from './util.js'; | ||
|
||
async function buildLibrary(dir: string) { | ||
const debug = Debug('@joelbot/build:library'); | ||
|
||
const packageJson = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8')); | ||
if (packageJson.devDependencies?.typescript) { | ||
debug('typescript is a devDependency, compiling to ESM and CommonJS'); | ||
|
||
fs.rmSync(path.join(dir, 'dist'), { force: true, recursive: true }); | ||
|
||
const options: Partial<Parameters<typeof esbuild.build>[0]> = { | ||
entryPoints: [path.join(dir, 'lib/**/*.ts')], | ||
outdir: path.join(dir, 'dist'), | ||
}; | ||
|
||
const tsconfigPath = path.join(os.tmpdir(), 'tsconfig.json'); | ||
debug('temporary tsconfigPath', tsconfigPath); | ||
|
||
const rootDir = path.join(dir, 'lib'); | ||
const outDir = path.join(dir, 'dist'); | ||
debug('rootDir', rootDir); | ||
debug('outDir', outDir); | ||
|
||
fs.writeFileSync( | ||
tsconfigPath, | ||
JSON.stringify( | ||
{ | ||
compilerOptions: { | ||
incremental: false, | ||
target: 'ESNext', | ||
jsx: 'react', | ||
moduleDetection: 'auto', | ||
moduleResolution: 'NodeNext', | ||
module: 'NodeNext', | ||
allowArbitraryExtensions: true, | ||
declaration: true, | ||
declarationMap: true, | ||
sourceMap: false, | ||
rootDir, | ||
outDir, | ||
emitDeclarationOnly: true, | ||
removeComments: false, | ||
importsNotUsedAsValues: 'remove', | ||
downlevelIteration: true, | ||
emitBOM: true, | ||
newLine: 'lf', | ||
stripInternal: true, | ||
noEmitOnError: true, | ||
isolatedModules: true, | ||
allowSyntheticDefaultImports: true, | ||
allowImportingTsExtensions: false, | ||
esModuleInterop: true, | ||
forceConsistentCasingInFileNames: true, | ||
strict: true, | ||
skipLibCheck: true, | ||
}, | ||
include: [path.join(rootDir, '**/*')], | ||
}, | ||
null, | ||
2, | ||
), | ||
); | ||
|
||
try { | ||
await Promise.all([ | ||
// declaration only typescript build | ||
spawn('yarn', ['tsc', '-p', tsconfigPath], { | ||
stdio: 'inherit', | ||
}), | ||
// ESM | ||
esbuild.build({ | ||
...options, | ||
format: 'esm', | ||
}), | ||
// CJS | ||
esbuild.build({ | ||
...options, | ||
format: 'cjs', | ||
outExtension: { | ||
'.js': '.cjs', | ||
}, | ||
}), | ||
]); | ||
} catch (err) { | ||
process.exit(1); | ||
} | ||
} else { | ||
debug('typescript is not a devDependency, copying lib to dist'); | ||
debug('todo: esbuild js so we get esm+cjs output in dist dir, similar to ts lib output'); | ||
fs.cpSync(path.join(dir, 'lib'), path.join(dir, 'dist'), { recursive: true }); | ||
} | ||
} | ||
|
||
async function main() { | ||
const debug = Debug('@joelbot/build'); | ||
for await (const dir of util.packageDirs()) { | ||
debug('building in directory', dir); | ||
const packageJson = JSON.parse(fs.readFileSync(path.join(dir, 'package.json'), 'utf-8')); | ||
if (packageJson.private) { | ||
debug('package is private, skipping'); | ||
continue; | ||
} | ||
if (packageJson.publishConfig?.access !== 'public') { | ||
debug('package publishConfig.access is not "public", skipping'); | ||
continue; | ||
} | ||
if (packageJson.scripts?.build) { | ||
debug('custom build script defined in package.json, running that'); | ||
await spawn('yarn', ['build'], { | ||
stdio: 'inherit', | ||
cwd: dir, | ||
}); | ||
continue; | ||
} | ||
if (!fs.existsSync(path.join(dir, 'lib'))) { | ||
debug('lib directory does not exist - nothing to build, skipping'); | ||
continue; | ||
} | ||
await buildLibrary(dir); | ||
} | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Debug from 'debug'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as cp from 'child_process'; | ||
|
||
export async function* packageDirs() { | ||
const debug = Debug('@joelbot/build:util:packageDirs'); | ||
debug('finding workspaces from', process.cwd()); | ||
if (!fs.existsSync('package.json')) { | ||
debug('no package.json found, doing nothing'); | ||
return; | ||
} | ||
const packageJson = JSON.parse(fs.readFileSync('package.json', 'utf-8')); | ||
if ('workspaces' in packageJson) { | ||
debug('workspaces are enabled in package.json'); | ||
if (!fs.existsSync('yarn.lock')) { | ||
console.log('package.json has workspaces enabled, but no yarn.lock file in current directory, doing nothing. try running build from workspace root.'); | ||
// todo: allow running build from non-workspace root directory at some point | ||
return; | ||
} | ||
const yarn = cp.spawnSync('yarn', ['workspaces', 'info']); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const workspaces = Object.values(JSON.parse(yarn.stdout.toString())) as any; | ||
debug( | ||
'workspaces', // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
workspaces.map((ws: any) => ws.location), | ||
); | ||
for (const { location } of workspaces) { | ||
yield path.join(process.cwd(), location); | ||
} | ||
} else { | ||
debug('workspaces are not enabled in package.json, yielding current directory'); | ||
yield process.cwd(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@joelbot/build", | ||
"version": "0.0.0", | ||
"private": false, | ||
"description": "Build tools for JoelBot", | ||
"author": "Joel Cox <[email protected]>", | ||
"license": "0BSD", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/joelcox22/public-libs" | ||
}, | ||
"dependencies": { | ||
"@expo/spawn-async": "^1.7.2", | ||
"@joelbot/tsconfig": "^1.0.4", | ||
"debug": "^4.3.4", | ||
"esbuild": "^0.21.3", | ||
"tsc": "^2.0.4" | ||
}, | ||
"bin": { | ||
"build": "dist/build.js" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"release": { | ||
"branches": [ | ||
"main" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,6 @@ | |
"author": "Joel Cox <[email protected]>", | ||
"license": "0BSD", | ||
"type": "module", | ||
"scripts": { | ||
"build": "exit 0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/joelcox22/lint.git" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import * as React from 'react'; | ||
|
||
type AsyncCallback<T> = (abortSignal: AbortSignal) => Promise<T>; | ||
type AsyncResult<T> = [loading: true, error: undefined, result: undefined] | [loading: false, error: Error, result: undefined] | [loading: false, error: undefined, result: T]; | ||
type RiskyAsyncResult<T> = [loading: true, result: undefined] | [loading: false, result: T]; | ||
|
||
export function useAsync<T>(effect: AsyncCallback<T>, deps: React.DependencyList = []): AsyncResult<T> { | ||
const [state, setState] = React.useState<AsyncResult<T>>([true, undefined, undefined]); | ||
React.useEffect(() => { | ||
setState([true, undefined, undefined]); | ||
const abortController = new AbortController(); | ||
let aborted = false; | ||
effect(abortController.signal) | ||
.then((result) => { | ||
setState([false, undefined, result]); | ||
}) | ||
.catch((err) => { | ||
if (aborted) return; | ||
setState([false, err, undefined]); | ||
}); | ||
return () => { | ||
aborted = true; | ||
abortController.abort(); | ||
}; | ||
}, deps); | ||
return state; | ||
} | ||
|
||
export function useRiskyAsync<T>(effect: AsyncCallback<T>, deps: React.DependencyList = []): RiskyAsyncResult<T> { | ||
const [loading, error, result] = useAsync(effect, deps); | ||
if (error) throw error; | ||
return [loading, result] as RiskyAsyncResult<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"name": "@joelbot/hooks", | ||
"version": "0.0.0", | ||
"private": false, | ||
"description": "A collection of useful react hooks", | ||
"author": "Joel Cox <[email protected]>", | ||
"license": "0BSD", | ||
"type": "module", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/joelcox22/public-libs" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.4.5" | ||
}, | ||
"peerDependencies": { | ||
"react": "^18.0.0" | ||
}, | ||
"exports": { | ||
"use-async-effect": "./dist/use-async-effect.js" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"release": { | ||
"branches": [ | ||
"main" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,12 +10,9 @@ | |
"author": "Joel Cox <[email protected]>", | ||
"license": "0BSD", | ||
"type": "module", | ||
"scripts": { | ||
"build": "exit 0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/joelcox22/lint.git" | ||
"url": "https://github.com/joelcox22/public-libs.git" | ||
}, | ||
"dependencies": { | ||
"app-root-path": "^3.1.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,6 @@ | |
"author": "Joel Cox <[email protected]>", | ||
"license": "0BSD", | ||
"type": "module", | ||
"scripts": { | ||
"build": "exit 0" | ||
}, | ||
"dependencies": { | ||
"app-root-path": "^3.1.0", | ||
"debug": "^4.3.4" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,9 @@ | |
"author": "Joel Cox <[email protected]>", | ||
"license": "0BSD", | ||
"type": "module", | ||
"scripts": { | ||
"build": "exit 0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/joelcox22/lint.git" | ||
"url": "https://github.com/joelcox22/public-libs.git" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
|
Oops, something went wrong.