@leverege/build-tools 2.93.0-beta.5 → 2.93.0-beta.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leverege/build-tools",
3
- "version": "2.93.0-beta.5",
3
+ "version": "2.93.0-beta.8",
4
4
  "description": "A collection of build / support tools for Leverege developers",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -50,12 +50,13 @@
50
50
  "npm-link": "src/npm-link.sh",
51
51
  "npm-unlink": "src/npm-unlink.sh",
52
52
  "overwhelm": "src/overwhelm.mjs",
53
- "package-artifact": "src/package-artifact/command.mjs",
54
53
  "pkgck": "src/pkgck.sh",
55
54
  "pkglint": "src/pkglint.sh",
56
55
  "prepack": "src/prepack.mjs",
57
56
  "prune-git": "src/prune-git.sh",
57
+ "pull-artifact": "src/artifacts/pull-artifact.mjs",
58
58
  "pull-git": "src/pull-git.sh",
59
+ "push-artifact": "src/artifacts/push-artifact.mjs",
59
60
  "push-my-chart": "src/push-my-chart.mjs",
60
61
  "refresh-npm-token": "src/refresh-npm-token.mjs",
61
62
  "refresh-py-idx": "src/refresh-py-idx.sh",
@@ -0,0 +1,21 @@
1
+ import crypto from 'crypto'
2
+ import fs from 'fs'
3
+
4
+ class ArtifactPusher {
5
+ constructor( artifact ) {
6
+ this.artifact = artifact
7
+ }
8
+
9
+ async pull() {
10
+ throw new Error( 'Not implemented' )
11
+ }
12
+ }
13
+
14
+ class GenericArtifactPusher extends ArtifactPusher {
15
+
16
+ async pull() {
17
+ throw new Error( 'Not implemented' )
18
+ }
19
+ }
20
+
21
+ export { ArtifactPusher, GenericArtifactPusher }
@@ -1,7 +1,7 @@
1
1
  import crypto from 'crypto'
2
2
  import fs from 'fs'
3
3
 
4
- class ArtifactPackager {
4
+ class ArtifactPusher {
5
5
  constructor( artifact ) {
6
6
  this.artifact = artifact
7
7
  }
@@ -12,6 +12,7 @@ class ArtifactPackager {
12
12
  hash.update( fs.readFileSync( this.artifact.source_path ) )
13
13
  return hash.digest( 'base64url' )
14
14
  } catch ( error ) {
15
+ console.error( error )
15
16
  throw new Error( `Failed to calculate hash for artifact ${this.artifact.name}: ${error.message}` )
16
17
  }
17
18
  }
@@ -20,27 +21,24 @@ class ArtifactPackager {
20
21
  throw new Error( 'Not implemented' )
21
22
  }
22
23
 
23
- async build() {
24
+ async push() {
24
25
  throw new Error( 'Not implemented' )
25
26
  }
27
+ }
26
28
 
27
- async publish() {
28
- throw new Error( 'Not implemented' )
29
- }
29
+ class GenericArtifactPusher extends ArtifactPusher {
30
30
 
31
- async package() {
32
- if ( await this.exists() ) {
33
- console.log( 'Artifact already exists in the registry' )
31
+ async exists() {
32
+ const isDirectory = fs.statSync( this.artifact.source_path ).isDirectory()
33
+
34
+ // If source path is a directory, check whether the destination path alreadu exists in the registry
35
+ if ( isDirectory ) {
36
+ // For the time being, return false, and let gcloud skip existing files in upload-time.
37
+ // It's pretty slow, might be worth to break down the directory into individual artifacts
38
+ // and manage each one with its own pusher.
34
39
  return false
35
40
  }
36
- await this.build()
37
- return this.publish()
38
- }
39
- }
40
-
41
- class GenericArtifactPackager extends ArtifactPackager {
42
41
 
43
- async exists() {
44
42
  // Check if artifact already exists in the registry
45
43
  console.log( 'Checking if artifact already exists in the registry...' )
46
44
 
@@ -84,22 +82,37 @@ class GenericArtifactPackager extends ArtifactPackager {
84
82
  throw error
85
83
  }
86
84
  }
87
-
88
- async build() {
89
- return true
90
- }
91
85
 
92
- async publish() {
86
+ async push() {
93
87
  console.log( 'Publishing artifact to registry...' )
94
-
95
- const command = `gcloud artifacts generic upload \
96
- --project=${this.artifact.gcp_project_id} \
97
- --location=${this.artifact.gcp_location} \
98
- --repository=${this.artifact.gcp_artifact_registry_repository_name} \
99
- --package=${this.artifact.gcp_artifact_registry_package_name} \
100
- --version=${this.artifact.gcp_artifact_registry_package_version} \
101
- --source=${this.artifact.source_path} \
102
- --destination-path=${this.artifact.destination_path}`
88
+
89
+ if ( await this.exists() ) {
90
+ console.log( 'Artifact already exists in the registry' )
91
+ return false
92
+ }
93
+
94
+ console.log( 'Uploading artifact to registry...' )
95
+
96
+ let command = ''
97
+
98
+ command = `gcloud artifacts generic upload \
99
+ --project=${this.artifact.gcp_project_id} \
100
+ --location=${this.artifact.gcp_location} \
101
+ --repository=${this.artifact.gcp_artifact_registry_repository_name} \
102
+ --package=${this.artifact.gcp_artifact_registry_package_name} \
103
+ --version=${this.artifact.gcp_artifact_registry_package_version}`
104
+
105
+ const isDirectory = fs.statSync( this.artifact.source_path ).isDirectory()
106
+ if ( isDirectory ) {
107
+ command += ` --source-directory=${this.artifact.source_path}`
108
+ command += ' --skip-existing'
109
+ } else {
110
+ command += ` --source=${this.artifact.source_path}`
111
+ }
112
+
113
+ if ( this.artifact.destination_path ) {
114
+ command += ` --destination-path=${this.artifact.destination_path}`
115
+ }
103
116
 
104
117
  // console.log( 'Command:', command )
105
118
 
@@ -115,4 +128,4 @@ class GenericArtifactPackager extends ArtifactPackager {
115
128
  }
116
129
  }
117
130
 
118
- export { ArtifactPackager, GenericArtifactPackager }
131
+ export { ArtifactPusher, GenericArtifactPusher }
@@ -1,11 +1,13 @@
1
1
  import { assign, object, string, array, optional, enums, union } from 'superstruct'
2
2
 
3
- const ArtifactTypeEnum = enums( [ 'generic' ] )
3
+ const ArtifactTypeEnum = {
4
+ GENERIC : 'generic',
5
+ }
4
6
 
5
7
  // Superstruct schema for individual artifact configuration
6
- const PartialArtifactEntrySchema = object( {
8
+ const PartialArtifactSchema = object( {
7
9
  name : string(),
8
- type : optional( ArtifactTypeEnum ),
10
+ type : optional( enums( Object.values( ArtifactTypeEnum ) ) ),
9
11
  gcp_project_id : optional( string() ),
10
12
  gcp_location : optional( string() ),
11
13
  gcp_artifact_registry_repository_name : optional( string() ),
@@ -13,9 +15,9 @@ const PartialArtifactEntrySchema = object( {
13
15
  gcp_artifact_registry_package_version : optional( string() )
14
16
  } )
15
17
 
16
- const ArtifactEntrySchema = assign( PartialArtifactEntrySchema, object( {
18
+ const ArtifactSchema = assign( PartialArtifactSchema, object( {
17
19
  name : string(),
18
- type : ArtifactTypeEnum,
20
+ type : enums( Object.values( ArtifactTypeEnum ) ),
19
21
  gcp_project_id : string(),
20
22
  gcp_location : string(),
21
23
  gcp_artifact_registry_repository_name : string(),
@@ -23,15 +25,15 @@ const ArtifactEntrySchema = assign( PartialArtifactEntrySchema, object( {
23
25
  gcp_artifact_registry_package_version : string()
24
26
  } ) )
25
27
 
26
- const PartialGenericArtifactEntrySchema = assign( PartialArtifactEntrySchema, object( {
27
- type : enums( [ 'generic' ] ),
28
+ const PartialGenericArtifactSchema = assign( PartialArtifactSchema, object( {
29
+ type : enums( [ ArtifactTypeEnum.GENERIC ] ),
28
30
  source_path : string(),
29
31
  destination_path : string()
30
32
  } ) )
31
33
 
32
- const GenericArtifactEntrySchema = assign( ArtifactEntrySchema, object( {
34
+ const GenericArtifactSchema = assign( ArtifactSchema, object( {
33
35
  source_path : string(),
34
- destination_path : string()
36
+ destination_path : optional( string() )
35
37
  } ) )
36
38
 
37
39
  // Superstruct schema for defaults configuration
@@ -44,17 +46,18 @@ const DefaultsSchema = object( {
44
46
  } )
45
47
 
46
48
  // Superstruct schema for the complete artifacts configuration with defaults
47
- const ArtifactEntriesSchema = object( {
48
- defaults : DefaultsSchema,
49
- artifacts : array( union( [ PartialArtifactEntrySchema, PartialGenericArtifactEntrySchema ] ) )
49
+ const ArtifactsSchema = object( {
50
+ defaults : optional( DefaultsSchema ),
51
+ artifacts : array( union( [ PartialArtifactSchema, PartialGenericArtifactSchema ] ) )
50
52
  } )
51
53
 
52
- const FlattenedArtifactEntriesSchema = array( assign( union( [ ArtifactEntrySchema, GenericArtifactEntrySchema ] ) ) )
54
+ const FlattenedArtifactsSchema = array( assign( union( [ ArtifactSchema, GenericArtifactSchema ] ) ) )
53
55
 
54
56
  export {
55
- ArtifactEntriesSchema,
56
- FlattenedArtifactEntriesSchema,
57
- ArtifactEntrySchema,
57
+ ArtifactTypeEnum,
58
+ ArtifactsSchema,
59
+ FlattenedArtifactsSchema,
60
+ ArtifactSchema,
58
61
  DefaultsSchema,
59
- GenericArtifactEntrySchema,
62
+ GenericArtifactSchema,
60
63
  }
@@ -2,18 +2,18 @@ import { validate } from 'superstruct'
2
2
 
3
3
  import { parseYamlFile, warning } from '../Utils.mjs'
4
4
 
5
- import { ArtifactEntriesSchema, GenericArtifactEntrySchema } from './Structs.mjs'
5
+ import { ArtifactTypeEnum, ArtifactsSchema, GenericArtifactSchema } from './Structs.mjs'
6
6
 
7
- const getArtifactEntriesSchema = async ( artifactsConfigPath = './artifacts.yml' ) => {
8
- const artifactsConfig = await parseYamlFile( artifactsConfigPath )
9
- const [ error, result ] = validate( artifactsConfig, ArtifactEntriesSchema )
7
+ const getArtifactsSchema = async ( artifactsFilePath = './artifacts.yml' ) => {
8
+ const artifactsConfig = await parseYamlFile( artifactsFilePath )
9
+ const [ error, result ] = validate( artifactsConfig, ArtifactsSchema )
10
10
  if ( error ) {
11
11
  throw new Error( `Invalid artifacts.yml file: ${error.message}` )
12
12
  }
13
13
  return result
14
14
  }
15
15
 
16
- const flattenArtifactEntriesSchema = ( artifactsSchema ) => {
16
+ const flattenArtifactsSchema = ( artifactsSchema ) => {
17
17
  return artifactsSchema.artifacts.reduce( ( flattenedArtifacts, artifact ) => {
18
18
  const mergedArtifact = {
19
19
  ...artifactsSchema.defaults,
@@ -22,8 +22,8 @@ const flattenArtifactEntriesSchema = ( artifactsSchema ) => {
22
22
 
23
23
  let schema
24
24
  switch ( mergedArtifact.type ) {
25
- case 'generic':
26
- schema = GenericArtifactEntrySchema
25
+ case ArtifactTypeEnum.GENERIC:
26
+ schema = GenericArtifactSchema
27
27
  break
28
28
  default:
29
29
  warning( `Unsupported artifact type "${mergedArtifact.type}"` )
@@ -46,4 +46,4 @@ const flattenArtifactEntriesSchema = ( artifactsSchema ) => {
46
46
  }, [] )
47
47
  }
48
48
 
49
- export { getArtifactEntriesSchema, flattenArtifactEntriesSchema }
49
+ export { getArtifactsSchema, flattenArtifactsSchema }
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+
3
+ async function main() {
4
+ return null
5
+ }
6
+
7
+ main()
@@ -2,8 +2,9 @@
2
2
 
3
3
  import { warning } from '../Utils.mjs'
4
4
 
5
- import { getArtifactEntriesSchema, flattenArtifactEntriesSchema } from './Utils.mjs'
6
- import { GenericArtifactPackager } from './Packager.mjs'
5
+ import { getArtifactsSchema, flattenArtifactsSchema } from './Utils.mjs'
6
+ import { ArtifactTypeEnum } from './Structs.mjs'
7
+ import { GenericArtifactPusher } from './ArtifactPusher.mjs'
7
8
 
8
9
  async function main() {
9
10
 
@@ -13,8 +14,8 @@ async function main() {
13
14
  const artifactPackageVersion = process.argv[4]
14
15
 
15
16
  if ( artifactsYmlPath ) {
16
- const artifactsSchema = await getArtifactEntriesSchema( artifactsYmlPath )
17
- const flattenedArtifactsSchema = flattenArtifactEntriesSchema( artifactsSchema )
17
+ const artifactsSchema = await getArtifactsSchema( artifactsYmlPath )
18
+ const flattenedArtifactsSchema = flattenArtifactsSchema( artifactsSchema )
18
19
 
19
20
  // Open for unexpected matches if we end up re-using the same package name and version for
20
21
  // multiple artifacts spread across multiple repositories and/or projects.
@@ -23,14 +24,16 @@ async function main() {
23
24
  artifact.gcp_artifact_registry_package_version === artifactPackageVersion )
24
25
 
25
26
  if ( filteredArtifactSchemas.length === 0 ) {
26
- console.error( `No artifact entries found for ${artifactPackageName} version ${artifactPackageVersion} in ${artifactsYmlPath}` )
27
+ console.error( `No artifacts found for ${artifactPackageName} version ${artifactPackageVersion} in ${artifactsYmlPath}` )
27
28
  process.exit( 1 )
28
29
  }
30
+
31
+ // TODO: Use concurrency to package artifacts and provide summary of results.
29
32
  for ( const artifactSchema of filteredArtifactSchemas ) {
30
33
  let packager
31
34
  switch ( artifactSchema.type ) {
32
- case 'generic':
33
- packager = new GenericArtifactPackager( artifactSchema )
35
+ case ArtifactTypeEnum.GENERIC:
36
+ packager = new GenericArtifactPusher( artifactSchema )
34
37
  break
35
38
  default:
36
39
  warning( `Unsupported artifact type "${artifactSchema.type}"` )