Last active
February 8, 2023 18:01
-
-
Save antoine-lecomte/db85d01ad2e57d5e786a68f8d5294a99 to your computer and use it in GitHub Desktop.
Keep only needed resources from @pulumi/azure-native package (see https://github.com/pulumi/pulumi-azure-native/issues/1997)
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
import { readdir, rm, writeFile } from 'fs/promises' | |
interface ModulesToKeep { | |
[moduleName: string]: string[] | |
} | |
interface PackageToKeep { | |
[packagePath: string]: ModulesToKeep | |
} | |
/** | |
* You can indicate multiples packages | |
* Specify modules to keep (and optional version, the default version will be kept) | |
*/ | |
const packageModulesToKeep: PackageToKeep = { | |
'': { | |
// Main project | |
config: [], | |
types: [], | |
resources: [], | |
automation: [], | |
authorization: [], | |
managedidentity: [], | |
web: ['v20210301'], | |
logic: [], | |
}, | |
'node_modules/submodulename': { | |
// Sub-module have its own @pulumi/azure-native | |
config: [], | |
types: [], | |
authorization: ['v20200801preview'], | |
}, | |
'node_modules/submodulename2': { | |
// Sub-module have its own @pulumi/azure-native | |
config: [], | |
types: [], | |
insights: ['v20210501preview'], | |
}, | |
} | |
const azureNativeModuleFolder = `node_modules/@pulumi/azure-native` | |
const defaultEnumsFolder = `types/enums` | |
async function getPreviousVersionFolders(path: string, packagePath: string, moduleName: string) { | |
const moduleFolders = await getFolderNames(path) | |
const subVersionsToKeep = Object.values(packageModulesToKeep[packagePath][moduleName]) | |
const data = moduleFolders | |
.filter((x) => !subVersionsToKeep.includes(x)) | |
.filter((x) => { | |
const match = x.match(/v\d{8}(?:preview)?$/) | |
return match && match.length === 1 | |
}) | |
.map((x) => [path, x].join('/')) | |
return data | |
} | |
async function getFolderNames(path: string) { | |
const dirents = await readdir(path, { | |
withFileTypes: true, | |
}) | |
return dirents.filter((x) => x.isDirectory()).map((x) => x.name) | |
} | |
async function getFolders(infos: { rootPath: string; packagePath: string }) { | |
const modules = await getFolderNames(infos.rootPath) | |
const modulesToKeep = Object.keys(packageModulesToKeep[infos.packagePath]) | |
const foldersPromises = modules.map(async (module) => { | |
const moduleFolderPath = [infos.rootPath, module].join('/') | |
if (modulesToKeep.includes(module)) { | |
return await getPreviousVersionFolders(moduleFolderPath, infos.packagePath, module) | |
} | |
return moduleFolderPath | |
}) | |
const moduleFolders = await Promise.all(foldersPromises) | |
return moduleFolders.flat() | |
} | |
async function getFoldersToClean(packagePath: string) { | |
const rootFolder = [__dirname, packagePath, azureNativeModuleFolder].join('/').replace('//', '/') | |
const enumsFolder = [rootFolder, defaultEnumsFolder].join('/') | |
const getFoldersPromises: Promise<string[]>[] = [ | |
{ rootPath: rootFolder, packagePath }, | |
{ rootPath: enumsFolder, packagePath }, | |
].map(getFolders) | |
const folders = await Promise.all(getFoldersPromises) | |
return folders.flat() | |
} | |
export async function getAllFoldersToClean() { | |
const packagePaths = Object.keys(packageModulesToKeep) | |
const result = packagePaths.map(async (packagePath): Promise<string[]> => { | |
return await getFoldersToClean(packagePath) | |
}) | |
return Promise.all(result) | |
} | |
export async function cleanFoldersAsync(packagePath: string) { | |
const folders = await getFoldersToClean(packagePath) | |
const removeTasks = await Promise.all( | |
folders.map(async (folder) => { | |
const files = await readdir(folder, { | |
withFileTypes: true, | |
}) | |
return files.map((file) => { | |
const path = `${folder}/${file.name}` | |
return rm(path, { | |
recursive: true, | |
force: true, | |
}) | |
}) | |
}), | |
) | |
const removeTasksFlattened = removeTasks.flat() | |
await Promise.all(removeTasksFlattened) | |
await Promise.all( | |
folders.map(async (folder) => { | |
const newIndexFile = [folder, 'index.js'].join('/') | |
await writeFile(newIndexFile, 'Object.defineProperty(exports, "__esModule", { value: true });') | |
}), | |
) | |
} | |
export async function cleanAllFoldersAsync() { | |
const packagePaths = Object.keys(packageModulesToKeep) | |
return packagePaths.map(async (packagePath) => { | |
return await cleanFoldersAsync(packagePath) | |
}) | |
} | |
async function main() { | |
// Show which folders would be deleted | |
// const result = await getAllFoldersToClean() | |
// console.log(result) | |
// Really delete folders | |
cleanAllFoldersAsync() | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment