Skip to content

Commit

Permalink
builder: add listr and native glob
Browse files Browse the repository at this point in the history
  • Loading branch information
VandeurenGlenn committed Apr 26, 2024
1 parent 8cd9806 commit 3b61b51
Show file tree
Hide file tree
Showing 4 changed files with 1,207 additions and 342 deletions.
11 changes: 8 additions & 3 deletions builder/hashit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ class CompareStream extends Writable {
const readAndCache = (file) =>
new Promise((resolve) => {
const hash = createHash('SHA1')
const input = createReadStream(file)
input.pipe(hash).setEncoding('hex').pipe(new CacheStream(resolve))
try {
const input = createReadStream(file)
input.pipe(hash).setEncoding('hex').pipe(new CacheStream(resolve))
} catch (error) {
console.error(error)
}
})

class HashStream extends Readable {
Expand Down Expand Up @@ -91,8 +95,9 @@ export default async ({ project, files }) => {
originalHash = (await readFile(PROJECT_CACHE_PATH)).toString()
} catch (error) {}

let hash
promises = await Promise.all(promises)
const hash = await _createHash(promises.join())
hash = await _createHash(promises.join())
if (String(originalHash) !== String(hash.toString())) {
changed = true
await writeFile(PROJECT_CACHE_PATH, hash)
Expand Down
134 changes: 91 additions & 43 deletions builder/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { globby } from 'globby'
import { readFile, readdir, mkdir, open } from 'fs/promises'
import { join } from 'path'
import { readFile, readdir, mkdir, open, stat } from 'fs/promises'
import { join, parse } from 'path'
import { CACHE_PATH } from './constants.js'
import hashit from './hashit.js'
import { rollup } from 'rollup'
import { glob } from 'fs/promises'
import { execSync, spawn, spawnSync } from 'child_process'
import Listr from 'listr'

const orderedList = ['messages', 'addresses', 'lib']
const root = 'packages'
Expand All @@ -13,14 +14,14 @@ const build = async (root, project) =>
new Promise((resolve) => {
try {
const spawnee = spawn(`npm run build`, { cwd: join(process.cwd(), root, project), shell: true })
spawnee.stdout.on('data', (data) => {
console.log(data.toString())
})
// spawnee.stdout.on('data', (data) => {
// console.log(data.toString())
// })

spawnee.stderr.on('data', (data) => {
if (data.toString().includes('created')) {
}
})
// spawnee.stderr.on('data', (data) => {
// if (data.toString().includes('created')) {
// }
// })
spawnee.stderr.on('close', () => resolve())
} catch (error) {
console.log(error.message)
Expand All @@ -32,60 +33,107 @@ const build = async (root, project) =>

const transformWorkspaceDir = async (root) =>
await Promise.all(
(await readdir(root)).map(async (project) => {
(
await readdir(root)
).map(async (project) => {
let files = []
try {
files = await globby([`${root}/${project}/src/**`])
const _files = await glob([`${root}/${project}/src/**`])
for await (const file of _files) {
const stats = await stat(file)
if (stats.isFile()) files.push(file)
}
} catch (error) {
console.warn(`no files found, make sure all source files are in ${join(root, project, 'src')}`)
}
return { root, project, files }
})
)

try {
const fd = await open(CACHE_PATH)
await fd.close()
} catch (error) {
await mkdir(CACHE_PATH)
const checkCache = async () => {
try {
const fd = await open(CACHE_PATH)
await fd.close()
} catch (error) {
await mkdir(CACHE_PATH)
}
}
let projectDirs

const projectDirs = await transformWorkspaceDir(root)
const promises = []

const projects = []

for (const project of [...projectDirs]) {
if (orderedList.includes(project.project)) {
projectDirs.slice(projectDirs.indexOf(project), '1')
projects.push(project)
const buildProjects = async () => {
for (const project of [...projectDirs]) {
if (orderedList.includes(project.project)) {
projectDirs.slice(projectDirs.indexOf(project), '1')
projects.push(project)
}
}
}

try {
for (const project of [...projects, ...projectDirs]) {
try {
const result = await hashit(project)
if (result)
if (result.changed || process.argv.includes('--all')) {
if (orderedList.includes(result.project)) await build(root, result.project)
else promises.push(build(root, result.project))
try {
for (const project of [...projects, ...projectDirs]) {
try {
const result = await hashit(project)
if (result)
if (result.changed || process.argv.includes('--all')) {
if (orderedList.includes(result.project)) await build(root, result.project)
else promises.push(build(root, result.project))
}
} catch (error) {
if (error.message.includes('Missing script: "build"')) {
console.warn(`no npm run build command present for ${project}`)
}
} catch (error) {
if (error.message.includes('Missing script: "build"')) {
console.warn(`no npm run build command present for ${project}`)
console.log(error.message)
console.error(error)
}
console.log(error.message)
console.error(error)
}
await Promise.all(promises)
} catch (error) {
console.log(error.message)
if (error.message.includes('Missing script: "build"')) {
console.warn(`no npm run build command present for ${project}`)
}
console.error(error)
}
await Promise.all(promises)
} catch (error) {
console.log(error.message)
if (error.message.includes('Missing script: "build"')) {
console.warn(`no npm run build command present for ${project}`)
}
console.error(error)
}

// const result = await Promise.all(promises)
// console.log(result)
const tasks = new Listr([
{
title: 'Check requirements',
task: () =>
new Listr([
{
title: 'cache',
task: () => checkCache()
}
])
},
{
title: 'Get projects',
task: () =>
new Listr([
{
title: 'getting projects',
task: async () => (projectDirs = await transformWorkspaceDir(root))
}
])
},
{
title: 'Build projects',
task: () =>
new Listr([
{
title: 'building projects',
task: () => buildProjects()
}
])
}
])

tasks.run().catch((err) => {
console.error(err)
})
Loading

0 comments on commit 3b61b51

Please sign in to comment.