cli: added temp dir utility to create context

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-11-12 13:08:25 +01:00
parent 84936dbcbe
commit 14e8162d19
2 changed files with 32 additions and 7 deletions
+29 -7
View File
@@ -14,7 +14,9 @@
* limitations under the License.
*/
import os from 'os';
import fs from 'fs-extra';
import { join as joinPath } from 'path';
import { Command } from 'commander';
import { FactoryRegistry } from '../../lib/create/FactoryRegistry';
import { paths } from '../../lib/paths';
@@ -68,11 +70,31 @@ export default async (cmd: Command) => {
}
}
await factory.create(options, {
isMonoRepo,
defaultVersion,
scope: cmdOpts.scope.replace(/^@/, ''),
npmRegistry: cmdOpts.npmRegistry,
private: Boolean(cmdOpts.private),
});
const tempDirs = new Array<string>();
async function createTemporaryDirectory(name: string): Promise<string> {
const dir = await fs.mkdtemp(joinPath(os.tmpdir(), name));
tempDirs.push(dir);
return dir;
}
try {
await factory.create(options, {
isMonoRepo,
defaultVersion,
scope: cmdOpts.scope.replace(/^@/, ''),
npmRegistry: cmdOpts.npmRegistry,
private: Boolean(cmdOpts.private),
createTemporaryDirectory,
});
} finally {
for (const dir of tempDirs) {
try {
await fs.remove(dir);
} catch (error) {
console.error(
`Failed to remove temporary directory '${dir}', ${error}`,
);
}
}
}
};
+3
View File
@@ -27,6 +27,9 @@ export interface CreateContext {
isMonoRepo: boolean;
/** The default version to use for new packages */
defaultVersion: string;
/** Creates a temporary directory. This will always be deleted after creation is done. */
createTemporaryDirectory(name: string): Promise<string>;
}
export type AnyOptions = Record<string, string>;