diff --git a/server/cache-service.ts b/server/cache-service.ts new file mode 100644 index 0000000..0ea7a54 --- /dev/null +++ b/server/cache-service.ts @@ -0,0 +1,35 @@ +import { promisify } from 'util'; +import * as fs from 'fs'; +import * as path from 'path'; +import * as os from 'os'; +import artifactoryService from './artifactory-service'; + +const readdir = promisify(fs.readdir); + +export default { + tmpDir: path.join(os.homedir(), '.npmfrog', 'package-cache'), + + // tslint:disable-next-line:typedef + async fillCacheFromDisc() { + const promises = []; + const scopes = await readdir(this.tmpDir); + for (const scope of scopes) { + const packages = await readdir(path.join(this.tmpDir, scope)); + for (const packageName of packages) { + const versions = await readdir(path.join(this.tmpDir, scope, packageName)); + for (const version of versions) { + artifactoryService + .getPackageDetail({ scope, packageName, version }) + .then(() => { + console.log(`caching ${scope}/${packageName}@${version} from disc`); + }) + .catch(() => { + console.warn( + `${scope}/${packageName}@${version} on disc seems to be outdated. Is will not be cached.`, + ); + }); + } + } + } + }, +}; diff --git a/server/index.ts b/server/index.ts index 04f1856..48be770 100644 --- a/server/index.ts +++ b/server/index.ts @@ -6,6 +6,7 @@ import * as morgan from 'morgan'; import * as fs from 'fs-extra'; import configService from './config-service'; import artifactoryService from './artifactory-service'; +import cacheService from './cache-service'; const portNumber = 30001; @@ -117,3 +118,5 @@ app.get('/meta', (req, res) => { }); app.listen(portNumber); + +cacheService.fillCacheFromDisc();