@teambit/pkg 1.0.107 → 1.0.108

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.
Files changed (45) hide show
  1. package/dist/exceptions/scope-not-found.d.ts +2 -2
  2. package/dist/pack.cmd.d.ts +2 -2
  3. package/dist/pack.cmd.js +3 -4
  4. package/dist/pack.cmd.js.map +1 -1
  5. package/dist/package-generator.d.ts +1 -1
  6. package/dist/packer.d.ts +6 -6
  7. package/dist/packer.js +1 -2
  8. package/dist/packer.js.map +1 -1
  9. package/dist/pkg.composition.d.ts +2 -2
  10. package/dist/pkg.main.runtime.d.ts +8 -8
  11. package/dist/pkg.main.runtime.js +18 -22
  12. package/dist/pkg.main.runtime.js.map +1 -1
  13. package/dist/pkg.service.d.ts +4 -4
  14. package/dist/pkg.service.js +3 -3
  15. package/dist/pkg.service.js.map +1 -1
  16. package/dist/pkg.ui.runtime.js +4 -4
  17. package/dist/pkg.ui.runtime.js.map +1 -1
  18. package/dist/{preview-1703590665075.js → preview-1703647408454.js} +2 -2
  19. package/dist/publish.cmd.d.ts +1 -1
  20. package/dist/publish.cmd.js +2 -3
  21. package/dist/publish.cmd.js.map +1 -1
  22. package/dist/publisher.d.ts +1 -1
  23. package/dist/publisher.js +4 -6
  24. package/dist/publisher.js.map +1 -1
  25. package/dist/write-npm-ignore.js +1 -2
  26. package/dist/write-npm-ignore.js.map +1 -1
  27. package/index.ts +12 -0
  28. package/pack.task.ts +27 -0
  29. package/package-env-type.ts +9 -0
  30. package/package-generator.ts +33 -0
  31. package/package.fragment.ts +24 -0
  32. package/package.json +29 -36
  33. package/package.route.ts +32 -0
  34. package/packer.ts +206 -0
  35. package/pkg-artifact.ts +5 -0
  36. package/pkg.aspect.ts +7 -0
  37. package/pkg.graphql.ts +59 -0
  38. package/pkg.main.runtime.ts +496 -0
  39. package/prepare-packages.task.ts +85 -0
  40. package/publish-dry-run.task.ts +44 -0
  41. package/publish.task.ts +33 -0
  42. package/publisher.ts +153 -0
  43. package/tsconfig.json +16 -21
  44. package/types/asset.d.ts +15 -3
  45. package/write-npm-ignore.ts +20 -0
package/packer.ts ADDED
@@ -0,0 +1,206 @@
1
+ import fs from 'fs-extra';
2
+ import path from 'path';
3
+ import ssri from 'ssri';
4
+ import _ from 'lodash';
5
+ import { pack } from '@pnpm/plugin-commands-publishing';
6
+ import { ComponentFactory } from '@teambit/component';
7
+ import { ComponentResult, ArtifactDefinition } from '@teambit/builder';
8
+ import { Capsule, IsolatorMain } from '@teambit/isolator';
9
+ import { isSnap } from '@teambit/component-version';
10
+ import { ScopeMain } from '@teambit/scope';
11
+ import LegacyScope from '@teambit/legacy/dist/scope/scope';
12
+ import { checksumFile } from '@teambit/legacy/dist/utils';
13
+ import { Logger } from '@teambit/logger';
14
+ import pMap from 'p-map';
15
+ import isRelative from 'is-relative-path';
16
+
17
+ // @ts-ignore (for some reason the tsc -w not found this)
18
+ import { ScopeNotFound } from './exceptions/scope-not-found';
19
+
20
+ export type PackResult = Omit<ComponentResult, 'component'>;
21
+ export type PackResultWithId = PackResult & {
22
+ id: string;
23
+ };
24
+
25
+ export const DEFAULT_TAR_DIR_IN_CAPSULE = 'package-tar';
26
+ const PACK_CONCURRENCY = 10;
27
+ export const TAR_FILE_ARTIFACT_NAME = 'package tar file';
28
+
29
+ export type PackResultMetadata = {
30
+ pkgJson: Record<any, string>;
31
+ tarPath: string;
32
+ tarName: string;
33
+ checksum?: string;
34
+ integrity?: string;
35
+ };
36
+
37
+ export type PackWriteOptions = {
38
+ outDir?: string;
39
+ override?: boolean;
40
+ };
41
+
42
+ export type PackOptions = {
43
+ writeOptions: PackWriteOptions;
44
+ prefix?: boolean;
45
+ keep?: boolean;
46
+ loadScopeFromCache?: boolean;
47
+ dryRun?: boolean;
48
+ };
49
+
50
+ export class Packer {
51
+ constructor(
52
+ private isolator: IsolatorMain,
53
+ private logger: Logger,
54
+ private host: ComponentFactory,
55
+ private scope?: ScopeMain
56
+ ) {}
57
+
58
+ async packComponent(
59
+ componentId: string,
60
+ scopePath: string | undefined,
61
+ options: PackOptions
62
+ ): Promise<PackResultWithId> {
63
+ // By default do not load scope from cache when packing
64
+ const loadScopeFromCache =
65
+ options && options.loadScopeFromCache !== undefined ? !!options.loadScopeFromCache : false;
66
+ const legacyScope = scopePath ? await LegacyScope.load(scopePath, loadScopeFromCache) : this.scope?.legacyScope;
67
+ if (!legacyScope) {
68
+ throw new ScopeNotFound(scopePath);
69
+ }
70
+ const capsule = await this.getCapsule(componentId, legacyScope);
71
+ const res = await this.packCapsule(capsule, options.writeOptions, options.dryRun);
72
+
73
+ return Object.assign({}, _.omit(res, ['component']), { id: componentId });
74
+ }
75
+
76
+ async packMultipleCapsules(
77
+ capsules: Capsule[],
78
+ writeOptions: PackWriteOptions = { override: true },
79
+ dryRun = false,
80
+ omitFullTarPath = false
81
+ ): Promise<ComponentResult[]> {
82
+ // const description = `packing components${dryRun ? ' (dry-run)' : ''}`;
83
+ const results = pMap(
84
+ capsules,
85
+ (capsule) => {
86
+ return this.packCapsule(capsule, writeOptions, dryRun, omitFullTarPath);
87
+ },
88
+ { concurrency: PACK_CONCURRENCY }
89
+ );
90
+ return results;
91
+ }
92
+
93
+ async packCapsule(
94
+ capsule: Capsule,
95
+ writeOptions: PackWriteOptions = { override: true },
96
+ dryRun = false,
97
+ omitFullTarPath = false
98
+ ): Promise<ComponentResult> {
99
+ const concreteWriteOpts = writeOptions;
100
+ // Set the package-tar as out dir to easily read the tar later
101
+ concreteWriteOpts.outDir = concreteWriteOpts.outDir ?? DEFAULT_TAR_DIR_IN_CAPSULE;
102
+ const packResult = await this.pnpmPack(
103
+ capsule.path,
104
+ concreteWriteOpts.outDir || capsule.path,
105
+ concreteWriteOpts.override,
106
+ dryRun
107
+ );
108
+ const component = capsule.component;
109
+ const fieldsToRemove: string[] = [];
110
+ if (omitFullTarPath) {
111
+ fieldsToRemove.push('tarPath');
112
+ }
113
+ // TODO: @gilad please make sure to fix this type error now that I added lodash types
114
+ const metadata = _(packResult.metadata).omitBy(_.isUndefined).omit(fieldsToRemove).value() as any;
115
+
116
+ return {
117
+ component,
118
+ metadata,
119
+ errors: packResult.errors,
120
+ warnings: packResult.warnings,
121
+ startTime: packResult.startTime,
122
+ endTime: packResult.endTime,
123
+ };
124
+ }
125
+
126
+ async pnpmPack(cwd: string, outputPath: string, override = false, dryRun = false): Promise<PackResult> {
127
+ const startTime = Date.now();
128
+ const errors: string[] = [];
129
+ const warnings: string[] = [];
130
+
131
+ try {
132
+ const pkgJson = readPackageJson(cwd);
133
+ if (isSnap(pkgJson.version)) {
134
+ warnings.push(`"package.json at ${cwd}" contain a snap version which is not a valid semver, can't pack it`);
135
+ return { warnings, startTime, endTime: Date.now() };
136
+ }
137
+ const tgzName = await pack.handler({
138
+ argv: { original: [] },
139
+ dir: cwd,
140
+ rawConfig: {},
141
+ });
142
+ this.logger.debug(`successfully packed tarball at ${cwd}`);
143
+ const tgzOriginPath = path.join(cwd, tgzName);
144
+ let tarPath = path.join(outputPath, tgzName);
145
+ if (isRelative(tarPath)) {
146
+ tarPath = path.join(cwd, tarPath);
147
+ }
148
+ const metadata: PackResultMetadata = {
149
+ pkgJson,
150
+ tarPath,
151
+ tarName: tgzName,
152
+ };
153
+ if (tgzOriginPath !== tarPath && fs.pathExistsSync(tarPath)) {
154
+ if (override) {
155
+ warnings.push(`"${tarPath}" already exists, override it`);
156
+ fs.removeSync(tarPath);
157
+ } else {
158
+ errors.push(`"${tarPath}" already exists, use --override flag to override`);
159
+ return { metadata, errors, startTime, endTime: Date.now() };
160
+ }
161
+ }
162
+ if (tgzOriginPath !== tarPath && !dryRun) {
163
+ await fs.move(tgzOriginPath, tarPath);
164
+ }
165
+ if (!dryRun) {
166
+ const checksum = await checksumFile(tarPath);
167
+ metadata.checksum = checksum;
168
+ metadata.integrity = await calculateFileIntegrity(tarPath);
169
+ }
170
+ return { metadata, warnings, errors, startTime, endTime: Date.now() };
171
+ } catch (err: any) {
172
+ const errorMsg = `failed packing at ${cwd}`;
173
+ this.logger.error(`${errorMsg}`, err);
174
+ if (err.stderr) this.logger.error(`${err.stderr}`);
175
+ errors.push(`${errorMsg}\n${err.stderr || err.message}`);
176
+ return { errors, startTime, endTime: Date.now() };
177
+ }
178
+ }
179
+
180
+ getArtifactDefInCapsule(outDir?: string): ArtifactDefinition {
181
+ const rootDir = outDir || DEFAULT_TAR_DIR_IN_CAPSULE;
182
+ const def: ArtifactDefinition = {
183
+ name: TAR_FILE_ARTIFACT_NAME,
184
+ globPatterns: [`${rootDir}/*.tgz`],
185
+ };
186
+ return def;
187
+ }
188
+
189
+ private async getCapsule(componentIdStr: string, legacyScope: LegacyScope): Promise<Capsule> {
190
+ const componentId = await this.host.resolveComponentId(componentIdStr);
191
+ const network = await this.isolator.isolateComponents([componentId], { baseDir: this.host.path }, legacyScope);
192
+ const capsule = network.seedersCapsules.getCapsule(componentId);
193
+
194
+ if (!capsule) throw new Error(`capsule not found for ${componentId}`);
195
+ return capsule;
196
+ }
197
+ }
198
+
199
+ function readPackageJson(dir: string) {
200
+ const pkgJson = fs.readJsonSync(path.join(dir, 'package.json'));
201
+ return pkgJson;
202
+ }
203
+
204
+ async function calculateFileIntegrity(filePath: string): Promise<string> {
205
+ return ssri.fromData(await fs.readFile(filePath), { algorithms: ['sha512'] }).toString();
206
+ }
@@ -0,0 +1,5 @@
1
+ import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
2
+
3
+ export class PkgArtifact {
4
+ constructor(private artifacts: AbstractVinyl[]) {}
5
+ }
package/pkg.aspect.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { Aspect } from '@teambit/harmony';
2
+
3
+ export const PkgAspect = Aspect.create({
4
+ id: 'teambit.pkg/pkg',
5
+ });
6
+
7
+ export default PkgAspect;
package/pkg.graphql.ts ADDED
@@ -0,0 +1,59 @@
1
+ import { Component } from '@teambit/component';
2
+ import { Schema } from '@teambit/graphql';
3
+ import gql from 'graphql-tag';
4
+
5
+ import { PkgMain } from './pkg.main.runtime';
6
+
7
+ export function pkgSchema(pkg: PkgMain): Schema {
8
+ return {
9
+ typeDefs: gql`
10
+ extend type Component {
11
+ packageManifest: PackageManifest
12
+
13
+ # package name of the component.
14
+ packageName: String!
15
+ }
16
+
17
+ type TarDist {
18
+ tarball: String
19
+ shasum: String
20
+ }
21
+
22
+ type VersionsPackageManifest {
23
+ name: String
24
+ version: String
25
+ main: String
26
+ dependencies: JSONObject
27
+ devDependencies: JSONObject
28
+ peerDependencies: JSONObject
29
+ scripts: JSONObject
30
+ dist: TarDist
31
+ }
32
+
33
+ type PackageManifest {
34
+ name: String
35
+ distTags: JSONObject
36
+ externalRegistry: Boolean
37
+ versions(version: String): [VersionsPackageManifest]
38
+ }
39
+ `,
40
+ resolvers: {
41
+ Component: {
42
+ packageName: (component: Component) => {
43
+ return pkg.getPackageName(component);
44
+ },
45
+ packageManifest: (component: Component) => {
46
+ return pkg.getManifest(component);
47
+ },
48
+ },
49
+ PackageManifest: {
50
+ versions: (parent, { version }) => {
51
+ if (version) {
52
+ return parent.versions.filter((v) => v.version === version);
53
+ }
54
+ return parent.versions;
55
+ },
56
+ },
57
+ },
58
+ };
59
+ }