@diquattro/cfnassets 0.6.1

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 (178) hide show
  1. package/README.md +55 -0
  2. package/lib/cli/build.d.ts +3 -0
  3. package/lib/cli/build.d.ts.map +1 -0
  4. package/lib/cli/build.js +77 -0
  5. package/lib/cli/build.js.map +1 -0
  6. package/lib/cli/cfnassets.d.ts +3 -0
  7. package/lib/cli/cfnassets.d.ts.map +1 -0
  8. package/lib/cli/cfnassets.js +7 -0
  9. package/lib/cli/cfnassets.js.map +1 -0
  10. package/lib/core/rollup/makeRollupPackageStream.d.ts +16 -0
  11. package/lib/core/rollup/makeRollupPackageStream.d.ts.map +1 -0
  12. package/lib/core/rollup/makeRollupPackageStream.js +27 -0
  13. package/lib/core/rollup/makeRollupPackageStream.js.map +1 -0
  14. package/lib/core/rollup/rollupPackageDir.d.ts +14 -0
  15. package/lib/core/rollup/rollupPackageDir.d.ts.map +1 -0
  16. package/lib/core/rollup/rollupPackageDir.js +36 -0
  17. package/lib/core/rollup/rollupPackageDir.js.map +1 -0
  18. package/lib/core/rollup/rollupPackageEntries.d.ts +5 -0
  19. package/lib/core/rollup/rollupPackageEntries.d.ts.map +1 -0
  20. package/lib/core/rollup/rollupPackageEntries.js +35 -0
  21. package/lib/core/rollup/rollupPackageEntries.js.map +1 -0
  22. package/lib/core/zip/ZipAssetEntry.d.ts +9 -0
  23. package/lib/core/zip/ZipAssetEntry.d.ts.map +1 -0
  24. package/lib/core/zip/ZipAssetEntry.js +2 -0
  25. package/lib/core/zip/ZipAssetEntry.js.map +1 -0
  26. package/lib/core/zip/getFolderEntries.d.ts +8 -0
  27. package/lib/core/zip/getFolderEntries.d.ts.map +1 -0
  28. package/lib/core/zip/getFolderEntries.js +32 -0
  29. package/lib/core/zip/getFolderEntries.js.map +1 -0
  30. package/lib/core/zip/getPackageEntries.d.ts +12 -0
  31. package/lib/core/zip/getPackageEntries.d.ts.map +1 -0
  32. package/lib/core/zip/getPackageEntries.js +73 -0
  33. package/lib/core/zip/getPackageEntries.js.map +1 -0
  34. package/lib/core/zip/makeZipPackageStream.d.ts +5 -0
  35. package/lib/core/zip/makeZipPackageStream.d.ts.map +1 -0
  36. package/lib/core/zip/makeZipPackageStream.js +29 -0
  37. package/lib/core/zip/makeZipPackageStream.js.map +1 -0
  38. package/lib/core/zip/zipDir.d.ts +8 -0
  39. package/lib/core/zip/zipDir.d.ts.map +1 -0
  40. package/lib/core/zip/zipDir.js +24 -0
  41. package/lib/core/zip/zipDir.js.map +1 -0
  42. package/lib/index.d.ts +25 -0
  43. package/lib/index.d.ts.map +1 -0
  44. package/lib/index.js +26 -0
  45. package/lib/index.js.map +1 -0
  46. package/lib/internal/HashStream.d.ts +13 -0
  47. package/lib/internal/HashStream.d.ts.map +1 -0
  48. package/lib/internal/HashStream.js +28 -0
  49. package/lib/internal/HashStream.js.map +1 -0
  50. package/lib/internal/ProgressStream.d.ts +11 -0
  51. package/lib/internal/ProgressStream.d.ts.map +1 -0
  52. package/lib/internal/ProgressStream.js +25 -0
  53. package/lib/internal/ProgressStream.js.map +1 -0
  54. package/lib/internal/addBundleInfoToPackageJson.d.ts +6 -0
  55. package/lib/internal/addBundleInfoToPackageJson.d.ts.map +1 -0
  56. package/lib/internal/addBundleInfoToPackageJson.js +36 -0
  57. package/lib/internal/addBundleInfoToPackageJson.js.map +1 -0
  58. package/lib/internal/getPackagePath.d.ts +2 -0
  59. package/lib/internal/getPackagePath.d.ts.map +1 -0
  60. package/lib/internal/getPackagePath.js +11 -0
  61. package/lib/internal/getPackagePath.js.map +1 -0
  62. package/lib/internal/hash.d.ts +3 -0
  63. package/lib/internal/hash.d.ts.map +1 -0
  64. package/lib/internal/hash.js +9 -0
  65. package/lib/internal/hash.js.map +1 -0
  66. package/lib/internal/readDotIgnoreFile.d.ts +2 -0
  67. package/lib/internal/readDotIgnoreFile.d.ts.map +1 -0
  68. package/lib/internal/readDotIgnoreFile.js +13 -0
  69. package/lib/internal/readDotIgnoreFile.js.map +1 -0
  70. package/lib/internal/readDotIgnoreForFolder.d.ts +2 -0
  71. package/lib/internal/readDotIgnoreForFolder.d.ts.map +1 -0
  72. package/lib/internal/readDotIgnoreForFolder.js +6 -0
  73. package/lib/internal/readDotIgnoreForFolder.js.map +1 -0
  74. package/lib/internal/tryStat.d.ts +4 -0
  75. package/lib/internal/tryStat.d.ts.map +1 -0
  76. package/lib/internal/tryStat.js +13 -0
  77. package/lib/internal/tryStat.js.map +1 -0
  78. package/lib/template/AssetAttributes.d.ts +5 -0
  79. package/lib/template/AssetAttributes.d.ts.map +1 -0
  80. package/lib/template/AssetAttributes.js +2 -0
  81. package/lib/template/AssetAttributes.js.map +1 -0
  82. package/lib/template/AssetBase.d.ts +14 -0
  83. package/lib/template/AssetBase.d.ts.map +1 -0
  84. package/lib/template/AssetBase.js +34 -0
  85. package/lib/template/AssetBase.js.map +1 -0
  86. package/lib/template/AssetBuilder.d.ts +6 -0
  87. package/lib/template/AssetBuilder.d.ts.map +1 -0
  88. package/lib/template/AssetBuilder.js +2 -0
  89. package/lib/template/AssetBuilder.js.map +1 -0
  90. package/lib/template/AssetContext.d.ts +7 -0
  91. package/lib/template/AssetContext.d.ts.map +1 -0
  92. package/lib/template/AssetContext.js +12 -0
  93. package/lib/template/AssetContext.js.map +1 -0
  94. package/lib/template/AssetDefinition.d.ts +8 -0
  95. package/lib/template/AssetDefinition.d.ts.map +1 -0
  96. package/lib/template/AssetDefinition.js +2 -0
  97. package/lib/template/AssetDefinition.js.map +1 -0
  98. package/lib/template/AssetGenerator.d.ts +5 -0
  99. package/lib/template/AssetGenerator.d.ts.map +1 -0
  100. package/lib/template/AssetGenerator.js +2 -0
  101. package/lib/template/AssetGenerator.js.map +1 -0
  102. package/lib/template/AssetManifest.d.ts +13 -0
  103. package/lib/template/AssetManifest.d.ts.map +1 -0
  104. package/lib/template/AssetManifest.js +12 -0
  105. package/lib/template/AssetManifest.js.map +1 -0
  106. package/lib/template/AssetOutput.d.ts +7 -0
  107. package/lib/template/AssetOutput.d.ts.map +1 -0
  108. package/lib/template/AssetOutput.js +2 -0
  109. package/lib/template/AssetOutput.js.map +1 -0
  110. package/lib/template/AssetRef.d.ts +5 -0
  111. package/lib/template/AssetRef.d.ts.map +1 -0
  112. package/lib/template/AssetRef.js +2 -0
  113. package/lib/template/AssetRef.js.map +1 -0
  114. package/lib/template/CustomAsset.d.ts +7 -0
  115. package/lib/template/CustomAsset.d.ts.map +1 -0
  116. package/lib/template/CustomAsset.js +9 -0
  117. package/lib/template/CustomAsset.js.map +1 -0
  118. package/lib/template/DeploymentBundle.d.ts +16 -0
  119. package/lib/template/DeploymentBundle.d.ts.map +1 -0
  120. package/lib/template/DeploymentBundle.js +2 -0
  121. package/lib/template/DeploymentBundle.js.map +1 -0
  122. package/lib/template/FileSystemDeploymentBundle.d.ts +20 -0
  123. package/lib/template/FileSystemDeploymentBundle.d.ts.map +1 -0
  124. package/lib/template/FileSystemDeploymentBundle.js +72 -0
  125. package/lib/template/FileSystemDeploymentBundle.js.map +1 -0
  126. package/lib/template/ProgressStats.d.ts +6 -0
  127. package/lib/template/ProgressStats.d.ts.map +1 -0
  128. package/lib/template/ProgressStats.js +2 -0
  129. package/lib/template/ProgressStats.js.map +1 -0
  130. package/lib/template/makeAsset.d.ts +5 -0
  131. package/lib/template/makeAsset.d.ts.map +1 -0
  132. package/lib/template/makeAsset.js +6 -0
  133. package/lib/template/makeAsset.js.map +1 -0
  134. package/lib/template/makeAssetFromPackage.d.ts +12 -0
  135. package/lib/template/makeAssetFromPackage.d.ts.map +1 -0
  136. package/lib/template/makeAssetFromPackage.js +29 -0
  137. package/lib/template/makeAssetFromPackage.js.map +1 -0
  138. package/lib/template/processAssets.d.ts +4 -0
  139. package/lib/template/processAssets.d.ts.map +1 -0
  140. package/lib/template/processAssets.js +24 -0
  141. package/lib/template/processAssets.js.map +1 -0
  142. package/package.json +71 -0
  143. package/src/cli/build.ts +131 -0
  144. package/src/cli/cfnassets.ts +9 -0
  145. package/src/core/rollup/makeRollupPackageStream.ts +55 -0
  146. package/src/core/rollup/rollupPackageDir.ts +64 -0
  147. package/src/core/rollup/rollupPackageEntries.ts +46 -0
  148. package/src/core/zip/ZipAssetEntry.ts +11 -0
  149. package/src/core/zip/getFolderEntries.ts +47 -0
  150. package/src/core/zip/getPackageEntries.ts +104 -0
  151. package/src/core/zip/makeZipPackageStream.ts +41 -0
  152. package/src/core/zip/zipDir.ts +43 -0
  153. package/src/index.ts +25 -0
  154. package/src/internal/HashStream.ts +36 -0
  155. package/src/internal/ProgressStream.ts +32 -0
  156. package/src/internal/addBundleInfoToPackageJson.ts +52 -0
  157. package/src/internal/getPackagePath.ts +13 -0
  158. package/src/internal/hash.ts +9 -0
  159. package/src/internal/readDotIgnoreFile.ts +15 -0
  160. package/src/internal/readDotIgnoreForFolder.ts +8 -0
  161. package/src/internal/tryStat.ts +12 -0
  162. package/src/template/AssetAttributes.ts +5 -0
  163. package/src/template/AssetBase.ts +60 -0
  164. package/src/template/AssetBuilder.ts +8 -0
  165. package/src/template/AssetContext.ts +15 -0
  166. package/src/template/AssetDefinition.ts +8 -0
  167. package/src/template/AssetGenerator.ts +5 -0
  168. package/src/template/AssetManifest.ts +25 -0
  169. package/src/template/AssetOutput.ts +6 -0
  170. package/src/template/AssetRef.ts +4 -0
  171. package/src/template/CustomAsset.ts +11 -0
  172. package/src/template/DeploymentBundle.ts +20 -0
  173. package/src/template/FileSystemDeploymentBundle.ts +112 -0
  174. package/src/template/ProgressStats.ts +5 -0
  175. package/src/template/makeAsset.ts +12 -0
  176. package/src/template/makeAssetFromPackage.ts +42 -0
  177. package/src/template/processAssets.ts +40 -0
  178. package/src/types/loadConfigFile.d.ts +20 -0
@@ -0,0 +1,20 @@
1
+ import { Readable } from 'stream';
2
+
3
+ export interface AddAssetOpts {
4
+ keepName?: boolean;
5
+ }
6
+
7
+ export interface DeploymentBundleAsset {
8
+ name: string;
9
+ size: number;
10
+ createReadStream(encoding?: BufferEncoding): Readable;
11
+ }
12
+
13
+ export interface DeploymentBundle {
14
+ addAsset(
15
+ fileName: string,
16
+ stream: Readable,
17
+ opts?: AddAssetOpts,
18
+ ): PromiseLike<string>;
19
+ assets: DeploymentBundleAsset[];
20
+ }
@@ -0,0 +1,112 @@
1
+ import { createReadStream, createWriteStream } from 'fs';
2
+ import { mkdir, rename, stat } from 'fs/promises';
3
+ import { basename, dirname, extname, join } from 'path';
4
+ import { Readable } from 'stream';
5
+ import { pipeline } from 'stream/promises';
6
+ import { temporaryDirectory } from 'tempy';
7
+ import { TypedEmitter } from 'tiny-typed-emitter';
8
+ import { hash } from '../internal/hash.js';
9
+ import { HashStream } from '../internal/HashStream.js';
10
+ import { ProgressStream } from '../internal/ProgressStream.js';
11
+ import {
12
+ AddAssetOpts,
13
+ DeploymentBundle,
14
+ DeploymentBundleAsset,
15
+ } from './DeploymentBundle.js';
16
+ import { ProgressStats } from './ProgressStats.js';
17
+
18
+ export interface FileSystemBundleOptions {
19
+ stagingDirectory?: string;
20
+ }
21
+
22
+ export interface FileSystemDeploymentBundleEvents {
23
+ assetProgress(name: string, stats: ProgressStats): void;
24
+ assetRename(oldName: string, rename: string): void;
25
+ }
26
+
27
+ export class FileSystemDeploymentBundle
28
+ extends TypedEmitter<FileSystemDeploymentBundleEvents>
29
+ implements DeploymentBundle
30
+ {
31
+ private readonly outputDir: string;
32
+ public readonly assets: DeploymentBundleAsset[] = [];
33
+
34
+ constructor(options: FileSystemBundleOptions) {
35
+ super();
36
+ this.outputDir = options.stagingDirectory ?? temporaryDirectory();
37
+ }
38
+
39
+ public async addAsset(
40
+ fileName: string,
41
+ content: Readable,
42
+ opts: AddAssetOpts = {},
43
+ ): Promise<string> {
44
+ if (dirname(fileName) !== '.') {
45
+ throw new Error(`the fileName must not be within a directory`);
46
+ }
47
+
48
+ this.emit('assetProgress', fileName, {});
49
+
50
+ const outDir = await this.getOutputDir();
51
+ const originalExt = extname(fileName);
52
+ const originalBasename = basename(fileName, originalExt);
53
+ const stagingExt = `.stagingprogress_${hash([
54
+ `${Date.now()}`,
55
+ fileName,
56
+ `${Math.random()}`,
57
+ ])}`;
58
+
59
+ const outPath = join(outDir, fileName + stagingExt);
60
+ const outStream = createWriteStream(outPath);
61
+
62
+ const progress = new ProgressStream((total) =>
63
+ this.emit('assetProgress', fileName, { total }),
64
+ );
65
+ progress.on('finish', () =>
66
+ this.emit('assetProgress', fileName, { complete: true }),
67
+ );
68
+
69
+ if (opts.keepName) {
70
+ await pipeline(content, progress, outStream);
71
+
72
+ const finalPath = join(outDir, originalBasename + originalExt);
73
+ await rename(outPath, finalPath);
74
+
75
+ this.assets.push({
76
+ name: fileName,
77
+ size: (await stat(finalPath)).size,
78
+ createReadStream: (encoding) => createReadStream(finalPath, encoding),
79
+ });
80
+ return fileName;
81
+ } else {
82
+ const hashStream = new HashStream();
83
+
84
+ await pipeline(content, progress, hashStream, outStream);
85
+ const sha = hashStream.digest('hex');
86
+
87
+ const finalBasename = originalBasename + `.${sha}` + originalExt;
88
+ const finalPath = join(outDir, finalBasename);
89
+
90
+ await rename(outPath, finalPath);
91
+ this.emit('assetRename', fileName, finalBasename);
92
+
93
+ this.assets.push({
94
+ name: finalBasename,
95
+ size: (await stat(finalPath)).size,
96
+ createReadStream: (encoding) => createReadStream(finalPath, encoding),
97
+ });
98
+ return finalBasename;
99
+ }
100
+ }
101
+
102
+ private getOutputDir = (() => {
103
+ let created = false;
104
+ return async () => {
105
+ if (!created) {
106
+ await mkdir(this.outputDir, { recursive: true });
107
+ created = true;
108
+ }
109
+ return this.outputDir;
110
+ };
111
+ })();
112
+ }
@@ -0,0 +1,5 @@
1
+ export interface ProgressStats {
2
+ complete?: boolean;
3
+ progress?: number;
4
+ total?: number;
5
+ }
@@ -0,0 +1,12 @@
1
+ import { TemplateBuilder } from '@awboost/cfntemplate';
2
+ import { AssetAttributes } from './AssetAttributes.js';
3
+ import { AssetGenerator } from './AssetGenerator.js';
4
+ import { CustomAsset } from './CustomAsset.js';
5
+
6
+ export function makeAsset(
7
+ name: string,
8
+ generate: AssetGenerator,
9
+ ): [TemplateBuilder, AssetAttributes] {
10
+ const asset = new CustomAsset(name, generate);
11
+ return [asset, asset];
12
+ }
@@ -0,0 +1,42 @@
1
+ import { TemplateBuilder } from '@awboost/cfntemplate';
2
+ import { createReadStream } from 'fs';
3
+ import { readFile } from 'fs/promises';
4
+ import { basename, dirname, join } from 'path';
5
+ import { AssetAttributes } from './AssetAttributes.js';
6
+ import { AssetBase } from './AssetBase.js';
7
+ import { AssetOutput } from './AssetOutput.js';
8
+
9
+ export class PackageAsset extends AssetBase {
10
+ constructor(
11
+ name: string,
12
+ private readonly packagePath: string,
13
+ private readonly assetName: string,
14
+ ) {
15
+ super(name);
16
+ }
17
+
18
+ public override async generate(): Promise<AssetOutput> {
19
+ const pkg = JSON.parse(await readFile(this.packagePath, 'utf8'));
20
+
21
+ if (!pkg.bundles || !pkg.bundles[this.assetName]?.path) {
22
+ throw new Error(
23
+ `expected package.json to have a bundles entry for '${this.assetName}'`,
24
+ );
25
+ }
26
+ const assetPath = pkg.bundles[this.assetName].path;
27
+
28
+ return {
29
+ content: createReadStream(join(dirname(this.packagePath), assetPath)),
30
+ fileName: basename(assetPath),
31
+ };
32
+ }
33
+ }
34
+
35
+ export function makeAssetFromPackage(
36
+ name: string,
37
+ packagePath: string,
38
+ assetName: string,
39
+ ): [TemplateBuilder, AssetAttributes] {
40
+ const asset = new PackageAsset(name, packagePath, assetName);
41
+ return [asset, asset];
42
+ }
@@ -0,0 +1,40 @@
1
+ import {
2
+ BuilderContext,
3
+ Template,
4
+ TemplateFragment,
5
+ } from '@awboost/cfntemplate';
6
+ import { AssetContext } from './AssetContext.js';
7
+ import { AssetManifest, AssetManifestMetadataKey } from './AssetManifest.js';
8
+ import { DeploymentBundle } from './DeploymentBundle.js';
9
+
10
+ export async function processAssets(
11
+ template: Template,
12
+ ctx: BuilderContext,
13
+ bundle: DeploymentBundle,
14
+ ): Promise<Template> {
15
+ const assets = ctx.get(AssetContext).assets;
16
+ if (!assets?.length) {
17
+ return template;
18
+ }
19
+
20
+ const manifest: AssetManifest = {
21
+ assets: [],
22
+ };
23
+
24
+ for (const asset of assets) {
25
+ const output = await asset.generate();
26
+ const key = await bundle.addAsset(output.fileName, output.content);
27
+
28
+ manifest.assets.push({
29
+ bucketParam: asset.parameters.S3Bucket,
30
+ key,
31
+ keyParam: asset.parameters.S3Key,
32
+ name: asset.name,
33
+ });
34
+ }
35
+
36
+ return TemplateFragment.metadata(AssetManifestMetadataKey, manifest).build(
37
+ template,
38
+ ctx,
39
+ );
40
+ }
@@ -0,0 +1,20 @@
1
+ import { MergedRollupOptions, RollupWarning } from 'rollup';
2
+
3
+ declare module 'rollup/loadConfigFile' {
4
+ export interface BatchWarnings {
5
+ add: (warning: RollupWarning) => void;
6
+ readonly count: number;
7
+ flush: () => void;
8
+ readonly warningOccurred: boolean;
9
+ }
10
+
11
+ export interface LoadConfigFileResult {
12
+ options: MergedRollupOptions[];
13
+ warnings: BatchWarnings;
14
+ }
15
+
16
+ export default function loadAndParseConfigFile(
17
+ fileName: string,
18
+ commandOptions?: any,
19
+ ): Promise<LoadConfigFileResult>;
20
+ }