@zondax/cli
Advanced tools
Comparing version 0.0.16 to 0.0.18
{ | ||
"name": "@zondax/cli", | ||
"version": "0.0.16", | ||
"version": "0.0.18", | ||
"description": "Zondax CLI", | ||
@@ -35,2 +35,3 @@ "author": "Zondax AG <hello@zondax.ch>", | ||
"@aws-sdk/client-s3": "^3.278.0", | ||
"axios": "^1.3.4", | ||
"commander": "^10.0.0", | ||
@@ -37,0 +38,0 @@ "create-temp-directory": "^2.3.0", |
import { processTemplateSection } from '../tools/templates' | ||
import { TemplateConfig } from '../types' | ||
describe('apply example repo', () => { | ||
describe('apply example git repo', () => { | ||
test('clone repo', async () => { | ||
const templateName = 'poc-template' | ||
const templateConfig: TemplateConfig = { | ||
@@ -15,3 +13,3 @@ global: { | ||
local: { | ||
template: templateName, | ||
template: 'poc-template', | ||
values: {}, | ||
@@ -23,2 +21,20 @@ }, | ||
}) | ||
test('apply example local', async () => { | ||
const templateConfig: TemplateConfig = { | ||
global: { | ||
projectName: 'testProject', | ||
values: {}, | ||
templates: [], | ||
}, | ||
local: { | ||
template: 'local://src/testcases', | ||
values: { | ||
field1: 123, | ||
}, | ||
}, | ||
} | ||
await processTemplateSection('/tmp/npx', templateConfig) | ||
}) | ||
}) |
@@ -1,9 +0,6 @@ | ||
import path from 'path' | ||
import { applyAllTemplates } from '../tools/templates' | ||
export async function cmdUpdate() { | ||
const cwd = path.resolve('.') | ||
await applyAllTemplates(cwd) | ||
await applyAllTemplates() | ||
return 0 | ||
} |
@@ -6,4 +6,11 @@ import path from 'path' | ||
// Remove .git/* | ||
export const IGNORE_REGEX = [/^\.git\//, /^zondax.yaml$/, /^\.gitignore$/] | ||
export const IGNORE_REGEX = [ | ||
/^\.git\//, | ||
/^zondax.yaml$/, | ||
// /^\.gitignore$/ | ||
] | ||
export const PREFIX_LOCAL_TEMPLATE = 'local://' | ||
export const PREFIX_GIT_TEMPLATE = 'https://' | ||
export function getConfigFilename(): string { | ||
@@ -10,0 +17,0 @@ const cwd = path.resolve('.') |
#!/usr/bin/env ts-node | ||
import { Command } from 'commander' | ||
import { cmdImages } from './cmd/images' | ||
import { cmdInit } from './cmd/init' | ||
import { cmdKubeconfig } from './cmd/kubeconfig' | ||
import { cmdTest } from './cmd/testcmd' | ||
import { cmdUpdate } from './cmd/update' | ||
@@ -16,3 +16,3 @@ | ||
.command('init') | ||
.description('Initialize project') | ||
.description('Template - Init') | ||
.action(async () => { | ||
@@ -24,3 +24,3 @@ await cmdInit() | ||
.command('check') | ||
.description('checks that all settings are set') | ||
.description('checks all values') | ||
.action(async () => { | ||
@@ -32,3 +32,3 @@ await cmdInit() | ||
.command('update') | ||
.description('Update project') | ||
.description('updates template') | ||
.action(async () => { | ||
@@ -39,13 +39,13 @@ await cmdUpdate() | ||
program | ||
.command('test') | ||
.description('Test template') | ||
.command('kubeconfig') | ||
.description('Update all kubeconfigs') | ||
.action(async () => { | ||
await cmdTest() | ||
await cmdKubeconfig() | ||
}) | ||
program | ||
.command('kubeconfig') | ||
.description('Kubeconfig') | ||
.command('images') | ||
.description('show all container images') | ||
.action(async () => { | ||
await cmdKubeconfig() | ||
await cmdImages() | ||
}) | ||
@@ -52,0 +52,0 @@ |
@@ -1,54 +0,6 @@ | ||
import fs, { RmOptions } from 'fs' | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { GetObjectCommand, ListObjectsCommand, S3Client } from '@aws-sdk/client-s3' | ||
import { getFile, listBucket } from './s3' | ||
function getS3Config(endpoint: string, accessKey?: string, secretKey?: string) { | ||
const region = 'us-east-1' | ||
if (accessKey && secretKey) { | ||
return { | ||
region: region, | ||
endpoint: endpoint, | ||
forcePathStyle: true, | ||
credentials: { | ||
accessKeyId: accessKey, | ||
secretAccessKey: secretKey, | ||
}, | ||
} | ||
} | ||
// Based on https://github.com/aws/aws-sdk-js-v3/issues/2321#issuecomment-916336230 | ||
return { | ||
region: region, | ||
endpoint: endpoint, | ||
forcePathStyle: true, | ||
credentials: undefined, | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
signer: { sign: async request => request }, | ||
} | ||
} | ||
export async function listBucket(endpoint: string, bucket: string, accessKey?: string, secretKey?: string) { | ||
const config = getS3Config(endpoint, accessKey, secretKey) | ||
const s3 = new S3Client(config) | ||
const command = new ListObjectsCommand({ | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
Bucket: bucket, | ||
}) | ||
return await s3.send(command) | ||
} | ||
export async function getFile(endpoint: string, bucket: string, key: string, accessKey?: string, secretKey?: string) { | ||
const config = getS3Config(endpoint, accessKey, secretKey) | ||
const s3 = new S3Client(config) | ||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
const command = new GetObjectCommand({ Bucket: bucket, Key: key }) | ||
const response = await s3.send(command) | ||
return response.Body?.transformToString() | ||
} | ||
export async function installKubeconfig(outputDir: string, endpoint: string, bucket: string) { | ||
@@ -70,13 +22,1 @@ const response = await listBucket(endpoint, bucket) | ||
} | ||
export function deleteDirectory(path: string) { | ||
const options: RmOptions = { | ||
recursive: true, | ||
force: true, | ||
} | ||
fs.rm(path, options, err => { | ||
if (err) { | ||
console.log(`Error removing ${path}: ${err?.message} `) | ||
} | ||
}) | ||
} |
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { createTempDirectory } from 'create-temp-directory' | ||
import fs from 'fs' | ||
import fs, { existsSync } from 'fs' | ||
import Handlebars from 'handlebars' | ||
@@ -10,3 +10,3 @@ import git from 'isomorphic-git' | ||
import { IGNORE_REGEX, getConfigFilename } from '../config' | ||
import { IGNORE_REGEX, PREFIX_GIT_TEMPLATE, PREFIX_LOCAL_TEMPLATE, getConfigFilename } from '../config' | ||
import { GlobalConfig, TemplateConfig } from '../types' | ||
@@ -69,2 +69,3 @@ import { allFilesTemplate, ensureDirectory } from './tools' | ||
// TODO: improve function name | ||
async function applyTemplateSection(inputDir: string, outputDir: string, templateConfig: TemplateConfig) { | ||
@@ -75,5 +76,7 @@ const allFiles = await allFilesTemplate(inputDir, IGNORE_REGEX) | ||
// TODO: improve function name | ||
function applyTemplateFile(inputDir: string, outputDir: string, f: string, templateConfig: TemplateConfig) { | ||
const absInputFilename = path.join(inputDir, f) | ||
const absOutputFilename = path.join(outputDir, f) | ||
console.log(`[Applying] ${absOutputFilename}`) | ||
const renderedContent = renderTemplate(absInputFilename, templateConfig) | ||
@@ -83,17 +86,20 @@ | ||
fs.writeFileSync(absOutputFilename, renderedContent) | ||
} | ||
export async function processTemplateSection(outputDir: string, templateConfig: TemplateConfig) { | ||
if (templateConfig.local.template === '.') { | ||
// Run local template | ||
const cwd = path.resolve('./template') | ||
await applyTemplateSection(cwd, outputDir, templateConfig) | ||
return | ||
if (renderedContent.length == 0) { | ||
fs.rmSync(absOutputFilename) | ||
} | ||
} | ||
// TODO: refactor this so we can handle file:// and git:// or tpl:// ?? | ||
// else git clone | ||
// TODO: improve function name | ||
export async function processTemplateSectionLocal(outputDir: string, templateConfig: TemplateConfig) { | ||
// Run local template | ||
const templatePath = templateConfig.local.template.replaceAll(PREFIX_LOCAL_TEMPLATE, '') | ||
const cwd = path.resolve(templatePath) | ||
await applyTemplateSection(cwd, outputDir, templateConfig) | ||
} | ||
// TODO: improve function name | ||
export async function processTemplateSectionGit(outputDir: string, templateConfig: TemplateConfig) { | ||
const tmpDir = await createTempDirectory() | ||
try { | ||
const repoUrl = `https://git.zondax.dev/zondax/${templateConfig.local.template}.git` | ||
await git.clone({ | ||
@@ -103,3 +109,3 @@ fs, | ||
dir: tmpDir.path, | ||
url: repoUrl, | ||
url: templateConfig.local.template, | ||
singleBranch: true, | ||
@@ -119,6 +125,37 @@ depth: 1, | ||
export async function applyAllTemplates(outputDir: string) { | ||
// FIXME: Show error message if config is missing | ||
const config = loadConfigFile(getConfigFilename()) | ||
// TODO: improve function name | ||
export async function processTemplateSection(outputDir: string, templateConfig: TemplateConfig) { | ||
if (templateConfig.local.template.startsWith(PREFIX_LOCAL_TEMPLATE)) { | ||
return processTemplateSectionLocal(outputDir, templateConfig) | ||
} | ||
if (templateConfig.local.template.startsWith(PREFIX_GIT_TEMPLATE)) { | ||
return processTemplateSectionGit(outputDir, templateConfig) | ||
} | ||
// Default case, assume git.zondax.dev | ||
const patchedTemplateConfig = { | ||
...templateConfig, | ||
local: { | ||
...templateConfig.local, | ||
template: `https://git.zondax.dev/zondax/${templateConfig.local.template}.git`, | ||
}, | ||
} | ||
return processTemplateSectionGit(outputDir, patchedTemplateConfig) | ||
} | ||
// TODO: improve function name | ||
export async function applyAllTemplates() { | ||
const configFilename = getConfigFilename() | ||
if (!existsSync(configFilename)) { | ||
console.log('[ERROR] config file not found. (run init first?)') | ||
return | ||
} | ||
const config = loadConfigFile(configFilename) | ||
const outputDir = path.resolve(config.outputDir ?? '.') | ||
console.log(`[Output ] ${outputDir}`) | ||
// TODO: check that we don't have pending changes.. otherwise skip | ||
@@ -125,0 +162,0 @@ // - User is expected to commit or stash before this can be applied |
@@ -1,2 +0,2 @@ | ||
import fs, { readdirSync } from 'fs' | ||
import fs, { RmOptions, readdirSync } from 'fs' | ||
import Downloader from 'nodejs-file-downloader' | ||
@@ -58,1 +58,9 @@ import path, { join } from 'path' | ||
} | ||
export function deleteDirectory(path: string) { | ||
const options: RmOptions = { | ||
recursive: true, | ||
force: true, | ||
} | ||
fs.rmSync(path, options) | ||
} |
@@ -5,2 +5,3 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
values?: any | ||
outputDir?: string | ||
templates: SectionConfig[] | ||
@@ -7,0 +8,0 @@ } |
17016
16
467
9
+ Addedaxios@^1.3.4
+ Addedasynckit@0.4.0(transitive)
+ Addedaxios@1.7.9(transitive)
+ Addedcall-bind-apply-helpers@1.0.2(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedes-set-tostringtag@2.1.0(transitive)
+ Addedform-data@4.0.2(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.3.0(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedproxy-from-env@1.1.0(transitive)