@push.rocks/smartnpm 2.0.5 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@ import { PackageVersion, type IVersionData } from './smartnpm.classes.packagever
6
6
  export class NpmPackage {
7
7
  public static async createFromFullMetadataAndVersionData(
8
8
  npmRegistryArg: NpmRegistry,
9
- fullMetadataArg: plugins.packageJson.FullMetadata,
9
+ fullMetadataArg: Record<string, unknown>,
10
10
  versionsDataArg: {
11
11
  name: string;
12
12
  'dist-tags': { [key: string]: string };
@@ -34,30 +34,30 @@ export class NpmPackage {
34
34
  }
35
35
 
36
36
  // INSTANCE
37
- public name: string = null;
38
- public scope: string = null;
39
- public version: string = null;
40
- public allVersions: PackageVersion[];
41
- public allDistTags: PackageDisttag[];
42
- public description: string = null;
43
- public keywords: string[] = null;
44
- public date: string;
45
- public license: string;
46
- public links: {
37
+ public name: string | null = null;
38
+ public scope: string | null = null;
39
+ public version: string | null = null;
40
+ public allVersions: PackageVersion[] = [];
41
+ public allDistTags: PackageDisttag[] = [];
42
+ public description: string | null = null;
43
+ public keywords: string[] | null = null;
44
+ public date!: string;
45
+ public license!: string;
46
+ public links!: {
47
47
  npm: string;
48
48
  homepage: string;
49
49
  repository: string;
50
50
  bugs: string;
51
51
  };
52
- public author: {
52
+ public author!: {
53
53
  name: 'Lossless GmbH';
54
54
  };
55
- public publisher: {
55
+ public publisher!: {
56
56
  username: 'gitzone';
57
57
  email: 'npm@git.zone';
58
58
  };
59
- public maintainers: any = null;
60
- public dist: {
59
+ public maintainers: unknown = null;
60
+ public dist!: {
61
61
  integrity: string;
62
62
  shasum: string;
63
63
  tarball: string;
@@ -69,8 +69,8 @@ export class NpmPackage {
69
69
  popularity: number;
70
70
  maintenance: number;
71
71
  };
72
- } = null;
73
- public searchScore: number = null;
72
+ } | null = null;
73
+ public searchScore: number | null = null;
74
74
 
75
75
  public npmRegistryRef: NpmRegistry;
76
76
  constructor(npmRegistryArg: NpmRegistry) {
@@ -81,9 +81,7 @@ export class NpmPackage {
81
81
  * saves the package to disk
82
82
  */
83
83
  public async saveToDisk(targetDir: string) {
84
- const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
85
- const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(this.dist.tarball);
86
- await archive.exportToFs(targetDir);
84
+ await plugins.smartarchive.SmartArchive.create().url(this.dist.tarball).extract(targetDir);
87
85
  }
88
86
 
89
87
  /**
@@ -99,17 +97,15 @@ export class NpmPackage {
99
97
  optionsArg: {
100
98
  distTag?: string;
101
99
  version?: string;
102
- },
100
+ } = {},
103
101
  returnOnFirstArg = false
104
- ): Promise<plugins.smartfile.SmartFile[]> {
105
- const done = plugins.smartpromise.defer<plugins.smartfile.SmartFile[]>();
106
- const smartarchiveInstance = new plugins.smartarchive.SmartArchive();
107
- let tarballUrl = this.dist?.tarball;
102
+ ): Promise<plugins.smartfile.SmartFile[] | null> {
103
+ let tarballUrl: string | undefined = this.dist?.tarball;
108
104
  if (optionsArg?.version || optionsArg?.distTag) {
109
105
  if (optionsArg.distTag && optionsArg.version) {
110
106
  throw new Error('Please either specify version OR disttag, not both.');
111
107
  }
112
- let targetVersionString: string;
108
+ let targetVersionString: string | undefined;
113
109
  if (optionsArg.distTag) {
114
110
  const targetDistTag = this.allDistTags.find((distTag) => {
115
111
  return distTag.name === optionsArg.distTag;
@@ -120,6 +116,9 @@ export class NpmPackage {
120
116
  } else {
121
117
  targetVersionString = optionsArg.version;
122
118
  }
119
+ if (!targetVersionString) {
120
+ return null;
121
+ }
123
122
 
124
123
  // lets find the best matching release
125
124
  const bestMatchingVersion = this.getBestMatchingVersion(targetVersionString);
@@ -128,59 +127,20 @@ export class NpmPackage {
128
127
  }
129
128
  tarballUrl = this.allVersions.find(
130
129
  (packageVersion) => packageVersion.version === bestMatchingVersion
131
- ).dist.tarball;
130
+ )?.dist.tarball;
131
+ }
132
+ if (!tarballUrl) {
133
+ return null;
132
134
  }
133
- const archive = await plugins.smartarchive.SmartArchive.fromArchiveUrl(tarballUrl);
134
- const streamOfFiles = await archive.exportToStreamOfStreamFiles();
135
135
  const wantedFilePath = plugins.path.join('package', filePath);
136
-
137
- // Collect all stream files first
138
- const streamFileList: any[] = [];
139
-
140
- await new Promise<void>((resolve, reject) => {
141
- streamOfFiles.on('data', (streamFile) => {
142
- streamFileList.push(streamFile);
143
- });
144
-
145
- streamOfFiles.on('end', resolve);
146
- streamOfFiles.on('error', reject);
147
- });
148
-
149
- // Now process the collected files
150
- const allMatchingFiles: plugins.smartfile.SmartFile[] = [];
151
-
152
- for (const fileArg of streamFileList) {
153
- const filePath = fileArg.relativeFilePath || fileArg.path || '';
154
-
155
- // returnOnFirstArg requires exact match
156
- if (returnOnFirstArg && filePath === wantedFilePath) {
157
- try {
158
- const buffer = await fileArg.getContentAsBuffer();
159
- const smartFile = await plugins.smartfile.SmartFile.fromBuffer(
160
- filePath,
161
- buffer
162
- );
163
- done.resolve([smartFile]);
164
- return done.promise;
165
- } catch (error) {
166
- console.error('Error processing file:', error);
167
- }
168
- } else if (!returnOnFirstArg && filePath.startsWith(wantedFilePath)) {
169
- try {
170
- const buffer = await fileArg.getContentAsBuffer();
171
- const smartFile = await plugins.smartfile.SmartFile.fromBuffer(
172
- filePath,
173
- buffer
174
- );
175
- allMatchingFiles.push(smartFile);
176
- } catch (error) {
177
- console.error('Error processing file:', error);
178
- }
136
+ const allFiles = await plugins.smartarchive.SmartArchive.create().url(tarballUrl).toSmartFiles();
137
+ const allMatchingFiles = allFiles.filter((fileArg) => {
138
+ if (returnOnFirstArg) {
139
+ return fileArg.path === wantedFilePath;
179
140
  }
180
- }
181
-
182
- done.resolve(allMatchingFiles);
183
- return done.promise;
141
+ return fileArg.path.startsWith(wantedFilePath);
142
+ });
143
+ return returnOnFirstArg ? allMatchingFiles.slice(0, 1) : allMatchingFiles;
184
144
  }
185
145
 
186
146
  /**
@@ -192,9 +152,9 @@ export class NpmPackage {
192
152
  distTag?: string;
193
153
  version?: string;
194
154
  }
195
- ): Promise<plugins.smartfile.SmartFile> {
155
+ ): Promise<plugins.smartfile.SmartFile | null> {
196
156
  const result = await this.getFilesFromPackage(filePath, optionsArg, true);
197
- return result[0] || null;
157
+ return result?.[0] || null;
198
158
  }
199
159
 
200
160
  /**
@@ -203,7 +163,7 @@ export class NpmPackage {
203
163
  update() {}
204
164
 
205
165
  /** */
206
- public getBestMatchingVersion(versionArg: string): string {
166
+ public getBestMatchingVersion(versionArg: string): string | null {
207
167
  // lets find the best matching release
208
168
  const targetVersion = plugins.smartversion.SmartVersion.fromFuzzyString(versionArg);
209
169
  const versionStrings = this.allVersions.map((packageVersion) => packageVersion.version);
@@ -13,12 +13,14 @@ export interface INpmRegistryConstructorOptions {
13
13
  }
14
14
 
15
15
  export class NpmRegistry {
16
- public options: INpmRegistryConstructorOptions;
16
+ public options: Required<INpmRegistryConstructorOptions>;
17
17
  public registryCache: RegistryCache;
18
+ public smartFs = new plugins.smartfs.SmartFs(new plugins.smartfs.SmartFsProviderNode());
19
+ public smartFileFactory = plugins.smartfile.SmartFileFactory.nodeFs();
18
20
  private searchDomain = 'https://api.npms.io/v2/search?q=';
19
21
 
20
22
  constructor(optionsArg: INpmRegistryConstructorOptions = {}) {
21
- const defaultOptions: INpmRegistryConstructorOptions = {
23
+ const defaultOptions: Required<INpmRegistryConstructorOptions> = {
22
24
  npmRegistryUrl: 'https://registry.npmjs.org',
23
25
  };
24
26
  this.options = {
@@ -46,10 +48,17 @@ export class NpmRegistry {
46
48
  registryUrl: this.options.npmRegistryUrl,
47
49
  allVersions: true,
48
50
  });
51
+ if (!fullMetadata) {
52
+ throw new Error(`Could not retrieve metadata for package ${packageName}.`);
53
+ }
49
54
  const npmPackage = await NpmPackage.createFromFullMetadataAndVersionData(
50
55
  this,
51
56
  fullMetadata,
52
- versionData as any
57
+ versionData as {
58
+ name: string;
59
+ 'dist-tags': { [key: string]: string };
60
+ versions: { [key: string]: import('./smartnpm.classes.packageversion.js').IVersionData };
61
+ }
53
62
  );
54
63
  return npmPackage;
55
64
  }
@@ -60,7 +69,7 @@ export class NpmRegistry {
60
69
  * @param targetDir
61
70
  */
62
71
  public async savePackageToDisk(packageName: string, targetDir: string): Promise<void> {
63
- plugins.smartfile.fs.ensureDirSync(paths.nogitDir);
72
+ await this.smartFs.directory(paths.nogitDir).create();
64
73
  const npmPackage = await this.getPackageInfo(packageName);
65
74
  await npmPackage.saveToDisk(targetDir);
66
75
  }
@@ -75,7 +84,7 @@ export class NpmRegistry {
75
84
  distTag?: string;
76
85
  version?: string;
77
86
  }
78
- ): Promise<plugins.smartfile.SmartFile> {
87
+ ): Promise<plugins.smartfile.SmartFile | null> {
79
88
  // lets create a cache descriptor
80
89
  const cacheDescriptor: ICacheDescriptor = {
81
90
  registryUrl: this.options.npmRegistryUrl,
@@ -86,7 +95,7 @@ export class NpmRegistry {
86
95
  };
87
96
 
88
97
  // lets see if we have something cached
89
- const cachedFile: plugins.smartfile.SmartFile = await this.registryCache.getCachedFile(
98
+ const cachedFile = await this.registryCache.getCachedFile(
90
99
  cacheDescriptor
91
100
  );
92
101
 
@@ -98,9 +107,8 @@ export class NpmRegistry {
98
107
  (packageArg) => packageArg.name === 'latest'
99
108
  );
100
109
  if (!latestAvailable) {
101
- optionsArg = {
102
- version: npmPackage.getBestMatchingVersion('*'),
103
- };
110
+ const version = npmPackage.getBestMatchingVersion('*');
111
+ optionsArg = version ? { version } : undefined;
104
112
  }
105
113
  }
106
114
  const fileResult = await npmPackage.getFileFromPackage(filePathArg, optionsArg);
@@ -120,16 +128,15 @@ export class NpmRegistry {
120
128
  distTag?: string;
121
129
  version?: string;
122
130
  }
123
- ): Promise<plugins.smartfile.SmartFile[]> {
131
+ ): Promise<plugins.smartfile.SmartFile[] | null> {
124
132
  const npmPackage = await this.getPackageInfo(packageNameArg);
125
133
  if (!optionsArg?.version && !optionsArg?.distTag) {
126
134
  const latestAvailable = npmPackage.allDistTags.find(
127
135
  (packageDistTagArg) => packageDistTagArg.name === 'latest'
128
136
  );
129
137
  if (!latestAvailable) {
130
- optionsArg = {
131
- version: npmPackage.getBestMatchingVersion('*'),
132
- };
138
+ const version = npmPackage.getBestMatchingVersion('*');
139
+ optionsArg = version ? { version } : undefined;
133
140
  }
134
141
  }
135
142
  return npmPackage.getFilesFromPackage(filePath, optionsArg);
@@ -142,10 +149,10 @@ export class NpmRegistry {
142
149
  * TODO: rewrite as memory only
143
150
  */
144
151
  const baseDir = plugins.path.join(paths.nogitDir, packageNameArg.replace('/', '__'));
145
- await plugins.smartfile.fs.ensureDir(baseDir);
152
+ await this.smartFs.directory(baseDir).create();
146
153
  await this.savePackageToDisk(packageNameArg, baseDir);
147
- const virtualDir = await plugins.smartfile.VirtualDirectory.fromFsDirPath(baseDir);
148
- await plugins.smartfile.fs.remove(baseDir);
154
+ const virtualDir = await this.smartFileFactory.virtualDirectoryFromPath(baseDir);
155
+ await this.smartFs.directory(baseDir).recursive().delete();
149
156
  return virtualDir;
150
157
  }
151
158
 
@@ -224,7 +231,13 @@ export class NpmRegistry {
224
231
  `info: Search on npm for ${plugins.consolecolor.coloredString(searchString, 'pink')}`
225
232
  );
226
233
 
227
- let body: any;
234
+ let body: {
235
+ results?: Array<{
236
+ package: {
237
+ name: string;
238
+ };
239
+ }>;
240
+ } | string | undefined;
228
241
  try {
229
242
  const response = await plugins.smartrequest.SmartRequest.create()
230
243
  .url(this.searchDomain + searchString)
@@ -238,7 +251,7 @@ export class NpmRegistry {
238
251
  const packageArray: NpmPackage[] = [];
239
252
 
240
253
  // if request failed just return it empty
241
- if (!body || typeof body === 'string') {
254
+ if (!body || typeof body === 'string' || !Array.isArray(body.results)) {
242
255
  return packageArray;
243
256
  }
244
257
 
@@ -19,11 +19,11 @@ export class PackageVersion implements IVersionData {
19
19
  return packageVersion;
20
20
  }
21
21
 
22
- name: string;
23
- version: string;
24
- dependencies: { [key: string]: string };
25
- devDependencies: { [key: string]: string };
26
- dist: {
22
+ name!: string;
23
+ version!: string;
24
+ dependencies!: { [key: string]: string };
25
+ devDependencies!: { [key: string]: string };
26
+ dist!: {
27
27
  integrity: string;
28
28
  shasum: string;
29
29
  tarball: string;
@@ -12,6 +12,7 @@ export interface ICacheDescriptor {
12
12
  export class RegistryCache {
13
13
  npmregistryRef: NpmRegistry;
14
14
  public levelCache: plugins.levelcache.LevelCache;
15
+ public smartFileFactory = plugins.smartfile.SmartFileFactory.nodeFs();
15
16
 
16
17
  constructor(npmRegistryRefArg: NpmRegistry) {
17
18
  this.npmregistryRef = npmRegistryRefArg;
@@ -22,12 +23,12 @@ export class RegistryCache {
22
23
 
23
24
  public async getCachedFile(
24
25
  cacheDescriptorArg: ICacheDescriptor
25
- ): Promise<plugins.smartfile.SmartFile> {
26
+ ): Promise<plugins.smartfile.SmartFile | null> {
26
27
  const cacheEntry = await this.levelCache.retrieveCacheEntryByKey(
27
28
  this.getCacheDescriptorAsString(cacheDescriptorArg)
28
29
  );
29
30
  if (cacheEntry) {
30
- return plugins.smartfile.SmartFile.fromFoldedJson(cacheEntry.contents.toString());
31
+ return this.smartFileFactory.fromFoldedJson(cacheEntry.contents.toString());
31
32
  }
32
33
  return null;
33
34
  }
@@ -55,7 +56,7 @@ export class RegistryCache {
55
56
  }
56
57
  }
57
58
 
58
- public getCacheDescriptorAsString(cacheDescriptorArg?: ICacheDescriptor) {
59
+ public getCacheDescriptorAsString(cacheDescriptorArg: ICacheDescriptor) {
59
60
  return `${cacheDescriptorArg.registryUrl}//+//${cacheDescriptorArg.packageName}//+//${
60
61
  cacheDescriptorArg.filePath
61
62
  }//+//${cacheDescriptorArg.distTag || cacheDescriptorArg.version}`;
@@ -8,6 +8,7 @@ import * as consolecolor from '@push.rocks/consolecolor';
8
8
  import * as levelcache from '@push.rocks/levelcache';
9
9
  import * as smartarchive from '@push.rocks/smartarchive';
10
10
  import * as smartfile from '@push.rocks/smartfile';
11
+ import * as smartfs from '@push.rocks/smartfs';
11
12
  import * as smartpath from '@push.rocks/smartpath';
12
13
  import * as smartpromise from '@push.rocks/smartpromise';
13
14
  import * as smartrequest from '@push.rocks/smartrequest';
@@ -19,6 +20,7 @@ export {
19
20
  levelcache,
20
21
  smartarchive,
21
22
  smartfile,
23
+ smartfs,
22
24
  smartpath,
23
25
  smartpromise,
24
26
  smartrequest,