Skip to content

Commit

Permalink
feat(pack): adds pack command
Browse files Browse the repository at this point in the history
'd2-app-scripts pack' will create a zip-file based on either defaults
based on type 'app' or 'lib', or through arguments passed on the command
line.
  • Loading branch information
varl committed Jun 30, 2021
1 parent 6872ca0 commit be700f1
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
80 changes: 80 additions & 0 deletions cli/src/commands/pack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const fs = require('fs')
const path = require('path')
const { reporter, chalk, exit } = require('@dhis2/cli-helpers-engine')
const bundleApp = require('../lib/bundleApp')
const parseConfig = require('../lib/parseConfig')
const makePaths = require('../lib/paths')

exports.command = 'pack [folder]'

exports.describe = 'Create archive from the build.'

exports.builder = yargs =>
yargs
.positional('folder', {
describe: 'The folder to pack',
type: 'string',
defaultDescription: '.',
})
.option('destination', {
type: 'string',
describe: 'Directory to save the packed archive to.',
defaultDescription: '.',
})
.option('filename', {
type: 'string',
describe: 'Override the filename of the archive.',
defaultDescription: '{app-name}-{version}.zip',
})

exports.handler = async argv => {
const { cwd = process.cwd(), folder, destination, filename } = argv

const paths = makePaths(cwd)
const config = parseConfig(paths)

let inputPath, outputPath
if (config.type === 'app') {
inputPath = path.resolve(cwd, folder ? folder : paths.buildAppOutput)

outputPath = path.resolve(
cwd,
destination ? destination : paths.buildAppBundleOutput,
filename ? filename : paths.buildAppBundleFile
)
} else {
inputPath = path.resolve(cwd, folder ? folder : '.')

outputPath = path.resolve(
cwd,
destination ? destination : paths.buildLibBundleOutput,
filename ? filename : paths.buildLibBundleFile
)
}

if (!fs.existsSync(inputPath)) {
exit(
1,
`The folder ${path.relative(
cwd,
inputPath
)} does not exist. Maybe you forgot to build?`
)
}

// for scoped names in package.json
const clean = str => str.replace(/@/, '').replace(/\//, '-')

// replace placeholder within names defined in lib/paths.js
const archivePath = outputPath
.replace(/{name}/, clean(config.name))
.replace(/{version}/, config.version)

reporter.info(
`Creating archive from ${chalk.bold(
path.relative(cwd, inputPath)
)} at ${chalk.bold(path.relative(cwd, archivePath))}...`
)

await bundleApp(inputPath, archivePath)
}
8 changes: 7 additions & 1 deletion cli/src/lib/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ module.exports = (cwd = process.cwd()) => {
buildAppOutput: path.join(base, './build/app'),
buildAppManifest: path.join(base, './build/app/manifest.webapp'),
buildAppConfigJson: path.join(base, './build/app/d2.config.json'),
buildAppBundleFile: '{name}-{version}.zip',
buildAppBundleOutput: path.join(base, 'build', 'bundle'),
buildAppBundle: path.join(
base,
'./build/bundle/dhis2-{{name}}-{{version}}.zip'
'build',
'bundle',
'{name}-{version}.zip'
),
buildLibBundleFile: '{name}-{version}.zip',
buildLibBundleOutput: path.join(base),
}

reporter.debug('PATHS', paths)
Expand Down

0 comments on commit be700f1

Please sign in to comment.