Compare commits
43 Commits
Author | SHA1 | Date | |
---|---|---|---|
7737014464 | |||
349360cdc6 | |||
983a860e2f | |||
c375d9ba10 | |||
e874c935bb | |||
001a79252c | |||
634d1c3570 | |||
cd8c9bcdaa | |||
f6d3d0987b | |||
60a1de38ce | |||
0ef6d0ccb2 | |||
8e538fd84d | |||
6745115db7 | |||
5ebce389d3 | |||
b4100688ac | |||
77d515d915 | |||
aa71105b2d | |||
e0ccb6c076 | |||
af0d95f327 | |||
d0ac8f1418 | |||
62f8106fc6 | |||
e87b8c994b | |||
694ba7be25 | |||
952399c26e | |||
ab1e83b8bf | |||
67980f9f41 | |||
2d34397b9b | |||
2744e1a92b | |||
b8987b7913 | |||
95ffb06e77 | |||
71abbbb5f1 | |||
9dfa1a789e | |||
c68ea1c8fd | |||
8e943512f8 | |||
c010319076 | |||
cf03e58c6f | |||
d59875f23f | |||
6b13e46947 | |||
096c38de9e | |||
7292b45d33 | |||
3ddad53d29 | |||
617b49aaba | |||
2dcd93b431 |
@ -19,23 +19,36 @@ mirror:
|
|||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
audit:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
stage: security
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --production --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
|
tags:
|
||||||
|
- docker
|
||||||
|
allow_failure: true
|
||||||
|
|
||||||
|
auditDevDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
- npmci command npm audit --audit-level=high
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
@ -50,9 +63,7 @@ testStable:
|
|||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- priv
|
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
@ -63,9 +74,7 @@ testBuild:
|
|||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@ -85,6 +94,8 @@ release:
|
|||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g tslint typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -15,7 +15,7 @@
|
|||||||
"properties": {
|
"properties": {
|
||||||
"projectType": {
|
"projectType": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["website", "element", "service", "npm"]
|
"enum": ["website", "element", "service", "npm", "wcc"]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
24751
package-lock.json
generated
24751
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartnpm",
|
"name": "@pushrocks/smartnpm",
|
||||||
"version": "1.0.16",
|
"version": "1.0.39",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "interface with npm to retrieve package information",
|
"description": "interface with npm to retrieve package information",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -12,19 +12,23 @@
|
|||||||
"build": "(tsbuild --web)"
|
"build": "(tsbuild --web)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.24",
|
"@gitzone/tsbuild": "^2.1.25",
|
||||||
"@gitzone/tsrun": "^1.2.12",
|
"@gitzone/tsrun": "^1.2.12",
|
||||||
"@gitzone/tstest": "^1.0.33",
|
"@gitzone/tstest": "^1.0.54",
|
||||||
"@pushrocks/tapbundle": "^3.2.1",
|
"@pushrocks/tapbundle": "^3.2.14",
|
||||||
"@types/node": "^14.0.14",
|
"@types/node": "^15.0.2",
|
||||||
"tslint": "^6.1.2",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/consolecolor": "^2.0.1",
|
"@pushrocks/consolecolor": "^2.0.1",
|
||||||
"@pushrocks/smartarchive": "^1.0.12",
|
"@pushrocks/levelcache": "^1.0.10",
|
||||||
"@pushrocks/smartfile": "^7.0.12",
|
"@pushrocks/smartarchive": "^2.0.4",
|
||||||
"@pushrocks/smartrequest": "^1.1.47",
|
"@pushrocks/smartfile": "^8.0.10",
|
||||||
|
"@pushrocks/smartpromise": "^3.1.5",
|
||||||
|
"@pushrocks/smartrequest": "^1.1.51",
|
||||||
|
"@pushrocks/smarttime": "^3.0.38",
|
||||||
|
"@pushrocks/smartversion": "^2.0.7",
|
||||||
"package-json": "^6.5.0"
|
"package-json": "^6.5.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
@ -38,5 +42,8 @@
|
|||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -27,13 +27,6 @@ Platform support | [
|
|
||||||
> | By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy.html)
|
|
||||||
|
|
||||||
[](https://push.rocks)
|
|
||||||
|
|
||||||
## Contribution
|
## Contribution
|
||||||
|
|
||||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||||
|
28
test/test.ts
28
test/test.ts
@ -17,7 +17,7 @@ tap.test('should create valid instances', async () => {
|
|||||||
|
|
||||||
tap.test('should produce a valid search string and this return npmts', async () => {
|
tap.test('should produce a valid search string and this return npmts', async () => {
|
||||||
const packages = await npmRegistry.searchOnNpm({
|
const packages = await npmRegistry.searchOnNpm({
|
||||||
name: '@pushrocks/smartupdate'
|
name: '@pushrocks/smartupdate',
|
||||||
});
|
});
|
||||||
expect(packages[0].name).to.equal('@pushrocks/smartupdate');
|
expect(packages[0].name).to.equal('@pushrocks/smartupdate');
|
||||||
});
|
});
|
||||||
@ -25,22 +25,40 @@ tap.test('should produce a valid search string and this return npmts', async ()
|
|||||||
// lets test things with the verdaccio registry
|
// lets test things with the verdaccio registry
|
||||||
tap.test('should create a verdaccio registry', async () => {
|
tap.test('should create a verdaccio registry', async () => {
|
||||||
verdaccioRegistry = new NpmRegistry({
|
verdaccioRegistry = new NpmRegistry({
|
||||||
npmRegistryUrl: 'https://verdaccio.lossless.one'
|
npmRegistryUrl: 'https://verdaccio.lossless.one',
|
||||||
});
|
});
|
||||||
expect(verdaccioRegistry).to.be.instanceOf(smartnpm.NpmRegistry);
|
expect(verdaccioRegistry).to.be.instanceOf(smartnpm.NpmRegistry);
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should get package from verdaccio', async () => {
|
tap.test('should get package from verdaccio', async () => {
|
||||||
const npmPackage = await verdaccioRegistry.getPackageInfo('@pushrocks/smartupdate');
|
const npmPackage = await verdaccioRegistry.getPackageInfo('@pushrocks/smartupdate');
|
||||||
console.log(npmPackage);
|
|
||||||
expect(npmPackage.license).to.equal('MIT');
|
expect(npmPackage.license).to.equal('MIT');
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should get a specific file from a package', async () => {
|
tap.test('should get a specific file from a package', async () => {
|
||||||
const bundleFile = await verdaccioRegistry.getFileFromPackage(
|
const wantedFile = await verdaccioRegistry.getFileFromPackage(
|
||||||
'@pushrocks/websetup',
|
'@pushrocks/websetup',
|
||||||
'dist_bundle/bundle.js'
|
'ts/index.ts'
|
||||||
);
|
);
|
||||||
|
console.log(wantedFile.contentBuffer.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should get a specific file from a package', async () => {
|
||||||
|
const wantedFiles = await verdaccioRegistry.getFilesFromPackage(
|
||||||
|
'@pushrocks/websetup',
|
||||||
|
'ts/'
|
||||||
|
);
|
||||||
|
for(const file of wantedFiles) {
|
||||||
|
console.log(file.path);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should not get a nonexisting file from a package', async () => {
|
||||||
|
const wantedFileNotThere = await verdaccioRegistry.getFileFromPackage(
|
||||||
|
'@pushrocks/websetup',
|
||||||
|
'ts/notthere'
|
||||||
|
);
|
||||||
|
expect(wantedFileNotThere).to.be.null
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,13 +1,35 @@
|
|||||||
import * as plugins from './smartnpm.plugins';
|
import * as plugins from './smartnpm.plugins';
|
||||||
import { NpmRegistry } from './smartnpm.classes.npmregistry';
|
import { NpmRegistry } from './smartnpm.classes.npmregistry';
|
||||||
|
import { PackageDisttag } from './smartnpm.classes.packagedisttag';
|
||||||
|
import { PackageVersion, IVersionData } from './smartnpm.classes.packageversion';
|
||||||
|
|
||||||
export class NpmPackage {
|
export class NpmPackage {
|
||||||
public static async createFromFullMetadata(
|
public static async createFromFullMetadataAndVersionData(
|
||||||
npmRegistryArg: NpmRegistry,
|
npmRegistryArg: NpmRegistry,
|
||||||
fullMetadata: plugins.packageJson.FullMetadata
|
fullMetadataArg: plugins.packageJson.FullMetadata,
|
||||||
|
versionsDataArg: {
|
||||||
|
name: string;
|
||||||
|
'dist-tags': { [key: string]: string };
|
||||||
|
versions: { [key: string]: IVersionData };
|
||||||
|
}
|
||||||
) {
|
) {
|
||||||
const npmPackage = new NpmPackage(npmRegistryArg);
|
const npmPackage = new NpmPackage(npmRegistryArg);
|
||||||
Object.assign(npmPackage, fullMetadata);
|
Object.assign(npmPackage, fullMetadataArg);
|
||||||
|
npmPackage.allVersions = [];
|
||||||
|
npmPackage.allDistTags = [];
|
||||||
|
for (const versionArg of Object.keys(versionsDataArg.versions)) {
|
||||||
|
const packageVersion = PackageVersion.createFromVersionData(
|
||||||
|
versionsDataArg.versions[versionArg]
|
||||||
|
);
|
||||||
|
npmPackage.allVersions.push(packageVersion);
|
||||||
|
}
|
||||||
|
for (const distTagArg of Object.keys(versionsDataArg['dist-tags'])) {
|
||||||
|
const packageDistTag = new PackageDisttag(
|
||||||
|
distTagArg,
|
||||||
|
versionsDataArg['dist-tags'][distTagArg]
|
||||||
|
);
|
||||||
|
npmPackage.allDistTags.push(packageDistTag);
|
||||||
|
}
|
||||||
return npmPackage;
|
return npmPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -15,6 +37,8 @@ export class NpmPackage {
|
|||||||
public name: string = null;
|
public name: string = null;
|
||||||
public scope: string = null;
|
public scope: string = null;
|
||||||
public version: string = null;
|
public version: string = null;
|
||||||
|
public allVersions: PackageVersion[];
|
||||||
|
public allDistTags: PackageDisttag[];
|
||||||
public description: string = null;
|
public description: string = null;
|
||||||
public keywords: string[] = null;
|
public keywords: string[] = null;
|
||||||
public date: string;
|
public date: string;
|
||||||
@ -48,9 +72,9 @@ export class NpmPackage {
|
|||||||
} = null;
|
} = null;
|
||||||
public searchScore: number = null;
|
public searchScore: number = null;
|
||||||
|
|
||||||
public npmRegistry: NpmRegistry;
|
public npmRegistryRef: NpmRegistry;
|
||||||
constructor(npmRegistryArg: NpmRegistry) {
|
constructor(npmRegistryArg: NpmRegistry) {
|
||||||
this.npmRegistry = npmRegistryArg;
|
this.npmRegistryRef = npmRegistryArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,6 +82,106 @@ export class NpmPackage {
|
|||||||
*/
|
*/
|
||||||
public async saveToDisk(targetDir: string) {
|
public async saveToDisk(targetDir: string) {
|
||||||
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
||||||
await smartarchiveInstance.extractArchiveFromUrl(this.dist.tarball, targetDir);
|
await smartarchiveInstance.extractArchiveFromUrlToFs(this.dist.tarball, targetDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* saves the complete package to cache
|
||||||
|
*/
|
||||||
|
public async saveToCache() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get files from package
|
||||||
|
*/
|
||||||
|
public async getFilesFromPackage(
|
||||||
|
filePath: string,
|
||||||
|
optionsArg: {
|
||||||
|
distTag?: string;
|
||||||
|
version?: string;
|
||||||
|
},
|
||||||
|
returnOnFirstArg = false
|
||||||
|
): Promise<plugins.smartfile.Smartfile[]> {
|
||||||
|
const done = plugins.smartpromise.defer<plugins.smartfile.Smartfile[]>();
|
||||||
|
const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
|
||||||
|
let tarballUrl = this.dist?.tarball;
|
||||||
|
if (optionsArg?.version || optionsArg?.distTag) {
|
||||||
|
if (optionsArg.distTag && optionsArg.version) {
|
||||||
|
throw new Error('Please either specify version OR disttag, not both.');
|
||||||
|
}
|
||||||
|
let targetVersionString: string;
|
||||||
|
if (optionsArg.distTag) {
|
||||||
|
const targetDistTag = this.allDistTags.find((distTag) => {
|
||||||
|
return distTag.name === optionsArg.distTag;
|
||||||
|
});
|
||||||
|
if (targetDistTag) {
|
||||||
|
targetVersionString = targetDistTag.targetVersion;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
targetVersionString = optionsArg.version;
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets find the best matching release
|
||||||
|
const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
|
||||||
|
if (!bestMatchingVersion) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
tarballUrl = this.allVersions.find(
|
||||||
|
(packageVersion) => packageVersion.version === bestMatchingVersion
|
||||||
|
).dist.tarball;
|
||||||
|
}
|
||||||
|
const fileObservable = await smartarchiveInstance.extractArchiveFromUrlToObservable(tarballUrl);
|
||||||
|
const wantedFilePath = plugins.path.join('package', filePath);
|
||||||
|
const allMatchingFiles: plugins.smartfile.Smartfile[] = [];
|
||||||
|
const subscription = fileObservable.subscribe(
|
||||||
|
(fileArg) => {
|
||||||
|
// returnOnFirstArg requires exact match
|
||||||
|
if (returnOnFirstArg && fileArg.path === wantedFilePath) {
|
||||||
|
// lets resolve with the wanted file
|
||||||
|
done.resolve([fileArg]);
|
||||||
|
subscription.unsubscribe();
|
||||||
|
} else if(!returnOnFirstArg && fileArg.path.startsWith(wantedFilePath)) {
|
||||||
|
allMatchingFiles.push(fileArg);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
(err) => {
|
||||||
|
console.log(err);
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
done.resolve(allMatchingFiles);
|
||||||
|
subscription.unsubscribe();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
return done.promise;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get files from package
|
||||||
|
*/
|
||||||
|
public async getFileFromPackage(
|
||||||
|
filePath: string,
|
||||||
|
optionsArg?: {
|
||||||
|
distTag?: string;
|
||||||
|
version?: string;
|
||||||
|
}
|
||||||
|
): Promise<plugins.smartfile.Smartfile> {
|
||||||
|
const result = await this.getFilesFromPackage(filePath, optionsArg, true);
|
||||||
|
return result[0] || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* updates the package with information from the registry
|
||||||
|
*/
|
||||||
|
update() {}
|
||||||
|
|
||||||
|
/** */
|
||||||
|
public getBestMatchingVersion(versionArg: string): string {
|
||||||
|
// lets find the best matching release
|
||||||
|
const targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(versionArg);
|
||||||
|
const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
|
||||||
|
const bestMatchingVersion = targetVersion.getBestMatch(versionStrings);
|
||||||
|
if (!bestMatchingVersion) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return bestMatchingVersion;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import { ISearchObject } from './smartnpm.interfaces';
|
|||||||
|
|
||||||
// classes
|
// classes
|
||||||
import { NpmPackage } from './smartnpm.classes.npmpackage';
|
import { NpmPackage } from './smartnpm.classes.npmpackage';
|
||||||
|
import { ICacheDescriptor, RegistryCache } from './smartnpm.classes.registrycache';
|
||||||
|
|
||||||
export interface INpmRegistryConstructorOptions {
|
export interface INpmRegistryConstructorOptions {
|
||||||
npmRegistryUrl?: string;
|
npmRegistryUrl?: string;
|
||||||
@ -13,7 +14,7 @@ export interface INpmRegistryConstructorOptions {
|
|||||||
|
|
||||||
export class NpmRegistry {
|
export class NpmRegistry {
|
||||||
public options: INpmRegistryConstructorOptions;
|
public options: INpmRegistryConstructorOptions;
|
||||||
public registry: string;
|
public registryCache: RegistryCache;
|
||||||
private searchDomain = 'https://api.npms.io/v2/search?q=';
|
private searchDomain = 'https://api.npms.io/v2/search?q=';
|
||||||
|
|
||||||
constructor(optionsArg: INpmRegistryConstructorOptions = {}) {
|
constructor(optionsArg: INpmRegistryConstructorOptions = {}) {
|
||||||
@ -24,6 +25,7 @@ export class NpmRegistry {
|
|||||||
...defaultOptions,
|
...defaultOptions,
|
||||||
...optionsArg,
|
...optionsArg,
|
||||||
};
|
};
|
||||||
|
this.registryCache = new RegistryCache(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -34,8 +36,15 @@ export class NpmRegistry {
|
|||||||
const fullMetadata = await plugins.packageJson(packageName, {
|
const fullMetadata = await plugins.packageJson(packageName, {
|
||||||
registryUrl: this.options.npmRegistryUrl,
|
registryUrl: this.options.npmRegistryUrl,
|
||||||
fullMetadata: true,
|
fullMetadata: true,
|
||||||
|
}).catch(err => {
|
||||||
|
console.log(err);
|
||||||
|
return null;
|
||||||
});
|
});
|
||||||
const npmPackage = await NpmPackage.createFromFullMetadata(this, fullMetadata);
|
const versionData = await plugins.packageJson(packageName, {
|
||||||
|
registryUrl: this.options.npmRegistryUrl,
|
||||||
|
allVersions: true
|
||||||
|
});
|
||||||
|
const npmPackage = await NpmPackage.createFromFullMetadataAndVersionData(this, fullMetadata, versionData as any);
|
||||||
return npmPackage;
|
return npmPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,6 +54,7 @@ export class NpmRegistry {
|
|||||||
* @param targetDir
|
* @param targetDir
|
||||||
*/
|
*/
|
||||||
public async savePackageToDisk(packageName: string, targetDir: string): Promise<void> {
|
public async savePackageToDisk(packageName: string, targetDir: string): Promise<void> {
|
||||||
|
plugins.smartfile.fs.ensureDirSync(paths.nogitDir);
|
||||||
const npmPackage = await this.getPackageInfo(packageName);
|
const npmPackage = await this.getPackageInfo(packageName);
|
||||||
await npmPackage.saveToDisk(targetDir);
|
await npmPackage.saveToDisk(targetDir);
|
||||||
}
|
}
|
||||||
@ -52,15 +62,69 @@ export class NpmRegistry {
|
|||||||
/**
|
/**
|
||||||
* gets a file from a package as Smartfile
|
* gets a file from a package as Smartfile
|
||||||
*/
|
*/
|
||||||
public async getFileFromPackage(packageName: string, filePath: string) {
|
public async getFileFromPackage(packageNameArg: string, filePathArg: string, optionsArg?: {
|
||||||
const baseDir = plugins.path.join(paths.nogitDir, packageName.replace('/', '__'));
|
distTag?: string;
|
||||||
|
version?: string;
|
||||||
|
}): Promise<plugins.smartfile.Smartfile> {
|
||||||
|
// lets create a cache descriptor
|
||||||
|
const cacheDescriptor: ICacheDescriptor = {
|
||||||
|
registryUrl: this.options.npmRegistryUrl,
|
||||||
|
packageName: packageNameArg,
|
||||||
|
filePath: filePathArg,
|
||||||
|
distTag: optionsArg?.distTag,
|
||||||
|
version: optionsArg?.version
|
||||||
|
};
|
||||||
|
|
||||||
|
// lets see if we have something cached
|
||||||
|
const cachedFile: plugins.smartfile.Smartfile = await this.registryCache.getCachedFile(cacheDescriptor);
|
||||||
|
|
||||||
|
// lets handle both occasions
|
||||||
|
if (!cachedFile) {
|
||||||
|
const npmPackage = await this.getPackageInfo(packageNameArg);
|
||||||
|
if (!optionsArg?.version && !optionsArg?.distTag) {
|
||||||
|
const latestAvailable = npmPackage.allDistTags.find(packageArg => packageArg.name === 'latest');
|
||||||
|
if (!latestAvailable) {
|
||||||
|
optionsArg = {
|
||||||
|
version: npmPackage.getBestMatchingVersion('*')
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const fileResult = await npmPackage.getFileFromPackage(filePathArg, optionsArg);
|
||||||
|
if (fileResult) {
|
||||||
|
this.registryCache.cacheSmartFile(cacheDescriptor, fileResult);
|
||||||
|
}
|
||||||
|
return fileResult;
|
||||||
|
} else {
|
||||||
|
return cachedFile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getFilesFromPackage(packageNameArg: string, filePath: string, optionsArg?: {
|
||||||
|
distTag?: string;
|
||||||
|
version?: string;
|
||||||
|
}): Promise<plugins.smartfile.Smartfile[]> {
|
||||||
|
const npmPackage = await this.getPackageInfo(packageNameArg);
|
||||||
|
if (!optionsArg?.version && !optionsArg?.distTag) {
|
||||||
|
const latestAvailable = npmPackage.allDistTags.find(packageDistTagArg => packageDistTagArg.name === 'latest');
|
||||||
|
if (!latestAvailable) {
|
||||||
|
optionsArg = {
|
||||||
|
version: npmPackage.getBestMatchingVersion('*')
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return npmPackage.getFilesFromPackage(filePath, optionsArg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getPackageAsSmartfileVirtualDir(packageNameArg: string): Promise<plugins.smartfile.VirtualDirectory> {
|
||||||
|
/**
|
||||||
|
* TODO: rewrite as memory only
|
||||||
|
*/
|
||||||
|
const baseDir = plugins.path.join(paths.nogitDir, packageNameArg.replace('/', '__'));
|
||||||
await plugins.smartfile.fs.ensureDir(baseDir);
|
await plugins.smartfile.fs.ensureDir(baseDir);
|
||||||
await this.savePackageToDisk(packageName, baseDir);
|
await this.savePackageToDisk(packageNameArg, baseDir);
|
||||||
const smartfile = await plugins.smartfile.Smartfile.fromFilePath(
|
const virtualDir = await plugins.smartfile.VirtualDirectory.fromFsDirPath(baseDir);
|
||||||
plugins.path.join(baseDir, 'package', filePath)
|
|
||||||
);
|
|
||||||
await plugins.smartfile.fs.remove(baseDir);
|
await plugins.smartfile.fs.remove(baseDir);
|
||||||
return smartfile;
|
return virtualDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
11
ts/smartnpm.classes.packagedisttag.ts
Normal file
11
ts/smartnpm.classes.packagedisttag.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import * as plugins from './smartnpm.plugins';
|
||||||
|
|
||||||
|
export class PackageDisttag {
|
||||||
|
name: string;
|
||||||
|
targetVersion: string;
|
||||||
|
|
||||||
|
constructor(nameArg: string, targetVersionArg: string) {
|
||||||
|
this.name = nameArg;
|
||||||
|
this.targetVersion = targetVersionArg;
|
||||||
|
}
|
||||||
|
}
|
31
ts/smartnpm.classes.packageversion.ts
Normal file
31
ts/smartnpm.classes.packageversion.ts
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import * as plugins from './smartnpm.plugins';
|
||||||
|
|
||||||
|
export interface IVersionData {
|
||||||
|
name: string;
|
||||||
|
version: string;
|
||||||
|
dependencies: { [key: string]: string };
|
||||||
|
devDependencies: { [key: string]: string };
|
||||||
|
dist: {
|
||||||
|
integrity: string;
|
||||||
|
shasum: string;
|
||||||
|
tarball: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PackageVersion implements IVersionData {
|
||||||
|
public static createFromVersionData(versionDataArg: IVersionData) {
|
||||||
|
const packageVersion = new PackageVersion();
|
||||||
|
Object.assign(packageVersion, versionDataArg);
|
||||||
|
return packageVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
name: string;
|
||||||
|
version: string;
|
||||||
|
dependencies: { [key: string]: string };
|
||||||
|
devDependencies: { [key: string]: string };
|
||||||
|
dist: {
|
||||||
|
integrity: string;
|
||||||
|
shasum: string;
|
||||||
|
tarball: string;
|
||||||
|
};
|
||||||
|
}
|
50
ts/smartnpm.classes.registrycache.ts
Normal file
50
ts/smartnpm.classes.registrycache.ts
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import { NpmRegistry } from './smartnpm.classes.npmregistry';
|
||||||
|
import * as plugins from './smartnpm.plugins';
|
||||||
|
|
||||||
|
|
||||||
|
export interface ICacheDescriptor {
|
||||||
|
registryUrl: string;
|
||||||
|
packageName: string;
|
||||||
|
filePath: string;
|
||||||
|
distTag?: string;
|
||||||
|
version?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RegistryCache {
|
||||||
|
npmregistryRef: NpmRegistry;
|
||||||
|
public levelCache: plugins.levelcache.LevelCache;
|
||||||
|
|
||||||
|
constructor(npmRegistryRefArg: NpmRegistry) {
|
||||||
|
this.npmregistryRef = npmRegistryRefArg;
|
||||||
|
this.levelCache = new plugins.levelcache.LevelCache({
|
||||||
|
cacheId: encodeURIComponent(this.npmregistryRef.options.npmRegistryUrl),
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getCachedFile (cacheDescriptorArg: ICacheDescriptor): Promise<plugins.smartfile.Smartfile> {
|
||||||
|
const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg));
|
||||||
|
if (cacheEntry) {
|
||||||
|
return plugins.smartfile.Smartfile.fromFoldedJson(cacheEntry.contents.toString());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async cacheSmartFile (cacheDescriptorArg: ICacheDescriptor, smartfileArg: plugins.smartfile.Smartfile) {
|
||||||
|
if (smartfileArg && cacheDescriptorArg.version) {
|
||||||
|
await this.levelCache.storeCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg), new plugins.levelcache.CacheEntry({
|
||||||
|
contents: Buffer.from(smartfileArg.foldToJson()),
|
||||||
|
ttl: plugins.smarttime.getMilliSecondsFromUnits({hours: 1})
|
||||||
|
}));
|
||||||
|
} else {
|
||||||
|
await this.levelCache.storeCacheEntryByKey(this.getCacheDescriptorAsString(cacheDescriptorArg), new plugins.levelcache.CacheEntry({
|
||||||
|
contents: Buffer.from(smartfileArg.foldToJson()),
|
||||||
|
ttl: plugins.smarttime.getMilliSecondsFromUnits({minutes: 1})
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getCacheDescriptorAsString(cacheDescriptorArg?: ICacheDescriptor) {
|
||||||
|
return `${cacheDescriptorArg.registryUrl}//+//${cacheDescriptorArg.packageName}//+//${cacheDescriptorArg.filePath}//+//${cacheDescriptorArg.distTag || cacheDescriptorArg.version}`;
|
||||||
|
}
|
||||||
|
}
|
@ -2,4 +2,3 @@ import * as plugins from './smartnpm.plugins';
|
|||||||
|
|
||||||
export const packageDir = plugins.path.join(__dirname, '../');
|
export const packageDir = plugins.path.join(__dirname, '../');
|
||||||
export const nogitDir = plugins.path.join(packageDir, '.nogit/');
|
export const nogitDir = plugins.path.join(packageDir, '.nogit/');
|
||||||
plugins.smartfile.fs.ensureDirSync(nogitDir);
|
|
||||||
|
@ -5,11 +5,15 @@ export { path };
|
|||||||
|
|
||||||
// @pushrocks scope
|
// @pushrocks scope
|
||||||
import * as consolecolor from '@pushrocks/consolecolor';
|
import * as consolecolor from '@pushrocks/consolecolor';
|
||||||
|
import * as levelcache from '@pushrocks/levelcache';
|
||||||
import * as smartarchive from '@pushrocks/smartarchive';
|
import * as smartarchive from '@pushrocks/smartarchive';
|
||||||
import * as smartfile from '@pushrocks/smartfile';
|
import * as smartfile from '@pushrocks/smartfile';
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
|
import * as smartversion from '@pushrocks/smartversion';
|
||||||
|
import * as smarttime from '@pushrocks/smarttime';
|
||||||
|
|
||||||
export { consolecolor, smartarchive, smartfile, smartrequest };
|
export { consolecolor, levelcache, smartarchive, smartfile, smartpromise, smartrequest, smartversion, smarttime };
|
||||||
|
|
||||||
// third party scope
|
// third party scope
|
||||||
import packageJson from 'package-json';
|
import packageJson from 'package-json';
|
||||||
|
Reference in New Issue
Block a user