-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathpublish.ts
More file actions
166 lines (136 loc) · 6.22 KB
/
Copy pathpublish.ts
File metadata and controls
166 lines (136 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import path from 'path';
import * as os from 'os';
import { Argv } from 'yargs';
import { LogLevel, mapLogLevel } from '../../spec-utils/log';
import { rmLocal } from '../../spec-utils/pfs';
import { getPackageConfig } from '../../spec-utils/product';
import { createLog } from '../devContainers';
import { UnpackArgv } from '../devContainersSpecCLI';
import { doFeaturesPackageCommand } from './packageCommandImpl';
import { getCLIHost } from '../../spec-common/cliHost';
import { loadNativeModule } from '../../spec-common/commonUtils';
import { PackageCommandInput } from '../collectionCommonUtils/package';
import { getArchiveName, OCICollectionFileName } from '../collectionCommonUtils/packageCommandImpl';
import { publishOptions } from '../collectionCommonUtils/publish';
import { getCollectionRef, getRef, OCICollectionRef } from '../../spec-configuration/containerCollectionsOCI';
import { doPublishCommand, doPublishMetadata } from '../collectionCommonUtils/publishCommandImpl';
import { runAsyncHandler } from '../utils';
const collectionType = 'feature';
export function featuresPublishOptions(y: Argv) {
return publishOptions(y, 'feature');
}
export type FeaturesPublishArgs = UnpackArgv<ReturnType<typeof featuresPublishOptions>>;
export function featuresPublishHandler(args: FeaturesPublishArgs) {
runAsyncHandler(featuresPublish.bind(null, args));
}
async function featuresPublish({
'target': targetFolder,
'log-level': inputLogLevel,
'registry': registry,
'namespace': namespace
}: FeaturesPublishArgs) {
const disposables: (() => Promise<unknown> | undefined)[] = [];
const dispose = async () => {
await Promise.all(disposables.map(d => d()));
};
const pkg = getPackageConfig();
const cwd = process.cwd();
const cliHost = await getCLIHost(cwd, loadNativeModule, true);
const output = createLog({
logLevel: mapLogLevel(inputLogLevel),
logFormat: 'text',
log: (str) => process.stderr.write(str),
terminalDimensions: undefined,
}, pkg, new Date(), disposables);
const params = { output, env: process.env };
// Package features
const outputDir = path.join(os.tmpdir(), `/features-output-${Date.now()}`);
const packageArgs: PackageCommandInput = {
cliHost,
targetFolder,
outputDir,
output,
disposables,
forceCleanOutputDir: true,
};
const metadata = await doFeaturesPackageCommand(packageArgs);
if (!metadata) {
output.write(`(!) ERR: Failed to fetch ${OCICollectionFileName}`, LogLevel.Error);
process.exit(1);
}
let result = {};
for (const f of metadata.features) {
output.write(`Processing feature: ${f.id}...`, LogLevel.Info);
if (!f.version) {
output.write(`(!) WARNING: Version does not exist, skipping ${f.id}...`, LogLevel.Warning);
continue;
}
const resource = `${registry}/${namespace}/${f.id}`;
const featureRef = getRef(output, resource);
if (!featureRef) {
output.write(`(!) Could not parse provided Feature identifier: '${resource}'`, LogLevel.Error);
process.exit(1);
}
const archiveName = getArchiveName(f.id, collectionType);
// Properties here are available on the manifest without needing to download the full Feature archive.
const featureAnnotations = {
'dev.containers.metadata': JSON.stringify(f),
};
output.write(`Feature Annotations: ${JSON.stringify(featureAnnotations)}`, LogLevel.Debug);
const publishResult = await doPublishCommand(params, f.version, featureRef, outputDir, collectionType, archiveName, featureAnnotations);
if (!publishResult) {
output.write(`(!) ERR: Failed to publish '${resource}'`, LogLevel.Error);
process.exit(1);
}
const isPublished = (publishResult?.digest && publishResult?.publishedTags.length > 0);
let thisResult = isPublished ? {
...publishResult,
version: f.version,
} : {};
if (isPublished && f.legacyIds) {
output.write(`Processing legacyIds for '${f.id}'...`, LogLevel.Info);
let publishedLegacyIds: string[] = [];
for await (const legacyId of f.legacyIds) {
output.write(`Processing legacyId: '${legacyId}'...`, LogLevel.Info);
let legacyResource = `${registry}/${namespace}/${legacyId}`;
const legacyFeatureRef = getRef(output, legacyResource);
if (!legacyFeatureRef) {
output.write(`(!) Could not parse provided Feature identifier: '${legacyResource}'`, LogLevel.Error);
process.exit(1);
}
const publishResult = await doPublishCommand(params, f.version, legacyFeatureRef, outputDir, collectionType, archiveName, featureAnnotations);
if (!publishResult) {
output.write(`(!) ERR: Failed to publish '${legacyResource}'`, LogLevel.Error);
process.exit(1);
}
if (publishResult?.digest && publishResult?.publishedTags.length > 0) {
publishedLegacyIds.push(legacyId);
}
}
if (publishedLegacyIds.length > 0) {
thisResult = {
...thisResult,
publishedLegacyIds,
};
}
}
result = {
...result,
[f.id]: thisResult,
};
}
const featureCollectionRef: OCICollectionRef | undefined = getCollectionRef(output, registry, namespace);
if (!featureCollectionRef) {
output.write(`(!) Could not parse provided collection identifier with registry '${registry}' and namespace '${namespace}'`, LogLevel.Error);
process.exit(1);
}
if (! await doPublishMetadata(params, featureCollectionRef, outputDir, collectionType)) {
output.write(`(!) ERR: Failed to publish '${featureCollectionRef.registry}/${featureCollectionRef.path}'`, LogLevel.Error);
process.exit(1);
}
console.log(JSON.stringify(result));
// Cleanup
await rmLocal(outputDir, { recursive: true, force: true });
await dispose();
process.exit();
}