Skip to content

Commit

Permalink
Fix: Clear up workspace subdependencies (fixes #629)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster committed Feb 21, 2024
1 parent ab3fc17 commit 7687193
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
54 changes: 54 additions & 0 deletions lib/removeWorkspaceDuplicates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'fs'
import path from 'path'

function globToRegExp (glob) {
let rex = glob.replace(/\*\*/g, '.*')
rex = rex.replace(/\*/g, '[^/]*')
rex = rex.replace(/\\\\/g, '\/')
rex = rex.replace(/\./g, '\\.')
rex = rex.replace(/\//g, '\\/')
rex = `^${rex}$`
return new RegExp(rex, 'gm')
}

function unix(path) {
return path.replace(/\\/g,'/')
}

async function getAllSubDirs (dir, depth = Number.MAX_SAFE_INTEGER) {
async function collectPaths(dir, root = dir, level = 0) {
const subs = await fs.promises.readdir(dir)
const paths = []
for (let sub of subs) {
sub = path.resolve(dir, sub)
const relativeSub = unix(sub).replace(root + '/', '')
const stat = await fs.promises.stat(sub)
if (!stat || !stat.isDirectory()) continue
paths.push(relativeSub)
if (depth === level) continue
paths.push(...await collectPaths(sub, root, level+1))
}
return paths
}
const root = unix(path.resolve(process.cwd(), dir))
return collectPaths(root)
}

async function globs (glob, { cwd = process.cwd(), depth } = {}) {
const subDirs = await getAllSubDirs(cwd, depth)
const rex = globToRegExp(glob)
return subDirs.filter(subDir => rex.test(subDir))
}

async function cleanUpSubDependencies() {
const localModuleNames = await globs('*', { cwd: './local_adapt_modules', depth: 1 })
const subModules = await globs('adapt-authoring*/node_modules/adapt-authoring*', { cwd: 'node_modules/', depth: 3 })
for (let subModule of subModules) {
const subModuleName = subModule.split('/').pop()
if (!localModuleNames.includes(subModuleName)) continue
console.warn(`Removing erroneous sub dependency ${subModule}`)
await fs.promises.rm(`node_modules/${subModule}`, { recursive:true })
}
}

cleanUpSubDependencies()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"scripts": {
"debug": "node --inspect --preserve-symlinks ./bin/start",
"dev": "node ./bin/dev",
"start": "node ./bin/start"
"start": "node ./bin/start",
"postinstall": "node ./lib/removeWorkspaceDuplicates.js"
},
"dependencies": {
"adapt-authoring-adaptframework": "github:adapt-security/adapt-authoring-adaptframework",
Expand Down

0 comments on commit 7687193

Please sign in to comment.