Skip to content

Commit

Permalink
feat: add diff-versioned specs to index as separate export
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Jul 10, 2022
1 parent d30de9a commit 822a2a8
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions generate-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,58 @@ const normalize = (name) => {

const resolvedSrc = path.resolve(process.cwd(), 'src')

// ! This returns a tuple containig as first element a list of specs and as second element the diff-versioned specs of that list
const getSubfolderSpecNames = (dirPathRelativeToSrc) => {
const resolvedDirPath = path.join(resolvedSrc, dirPathRelativeToSrc)
try {
// if index.ts exists we are in a spec folder and we only return the current dir path e.g. fig/index.ts return fig
// if index.ts exists we are in a spec folder and we only return the current dir path e.g. fig/index.ts returns fig
fs.readFileSync(path.join(resolvedDirPath, 'index.ts'))
return [`"${dirPathRelativeToSrc}"`]
return Array(2).fill([`"${dirPathRelativeToSrc}"`])
} catch {
// otherwise the folder is just used to organize specs e.g. aws/*.ts
const specNames = []
const diffVersionedSpecNames = [];
for (const dirent of fs.readdirSync(resolvedDirPath, { withFileTypes: true })) {
if (dirent.isFile() && dirent.name.endsWith('.ts')) {
specNames.push(`"${
path.join(dirPathRelativeToSrc, dirent.name).slice(0, -3)
}"`)
} else if (dirent.isDirectory()) {
specNames.push(...getSubfolderSpecNames(path.join(dirPathRelativeToSrc, dirent.name)))
const [s, dvs] = getSubfolderSpecNames(path.join(dirPathRelativeToSrc, dirent.name))
console.error(s, '----', dvs)
specNames.push(...s)
diffVersionedSpecNames.push(...dvs)
}
}
return specNames
return [specNames, diffVersionedSpecNames]
}
}

const specNames = []
const specNames = [];
const diffVersionedSpecNames = [];

for (const dirent of fs.readdirSync(resolvedSrc, { withFileTypes: true })) {
if (dirent.isFile() && dirent.name.endsWith('.ts')) {
specNames.push(`"${
dirent.name.slice(0, -3)
}"`)
} else if (dirent.isDirectory()) {
specNames.push(...getSubfolderSpecNames(dirent.name))
const [s, dvs] = getSubfolderSpecNames(dirent.name)
specNames.push(...s)
diffVersionedSpecNames.push(...dvs)
}
}

fs.writeFileSync(
"build/index.js",
`var e=[${specNames.join(",")}];export{e as default};`
`var e=[${specNames.join(",")}],f=[${diffVersionedSpecNames.join(",")}];export{e as default,f};`
);

fs.writeFileSync(
"build/index.d.ts",
`
declare const completions: string[]
export { completions as default }
declare const diffVersionedCompletions: string[]
export { completions as default, diffVersionedCompletions }
`
);

0 comments on commit 822a2a8

Please sign in to comment.