1 | const path = require('path')
|
2 | const fs = require('fs-extra')
|
3 | const https = require('https')
|
4 | const tar = require('tar')
|
5 | const zlib = require('zlib')
|
6 | const Gauge = require('gauge')
|
7 | const chalk = require('chalk')
|
8 |
|
9 | module.exports = async ({ starterKit, env, program, pkg }) => {
|
10 | const graphicKits = ['storyboard', 'lg', 'panasonic', 'wedge', 'launcher']
|
11 | const gauge = new Gauge()
|
12 | const srcPath = path.join(program.cwd, './src')
|
13 | let version = pkg.starterKitVersion
|
14 | if (program.dev === true) {
|
15 | version = `master/${version}`
|
16 | } else if (program.dev) {
|
17 | version = `master/${program.dev}`
|
18 | }
|
19 | const url = `https://s3.amazonaws.com/starter-kits/${version}/starter-kit-${starterKit}.tar.gz`
|
20 | if (!!env.overwriteConfidently && !program.silent) {
|
21 | console.log('🚨 ', chalk.red('You are overwriting blink files. This voids your warranty as blink is likely not to function as intended.'))
|
22 | }
|
23 | try {
|
24 | await new Promise((resolve, reject) => {
|
25 | https.get(url, res => {
|
26 | const { statusCode, headers } = res
|
27 | if (statusCode === 200) {
|
28 | const total = headers['content-length']
|
29 | let count = 0
|
30 | res
|
31 | .on('data', data => {
|
32 | count += data.length
|
33 | if (!program.silent) {
|
34 | gauge.show(`Downloading starter-kit-${starterKit} ${version}`, count / total)
|
35 | }
|
36 | })
|
37 | .pipe(zlib.createGunzip())
|
38 | .pipe(tar.x({
|
39 | filter: (filename) => !(!!env.overwriteConfidently && ['.fla', '.toml', '.psd', '.psb'].includes(path.extname(filename))),
|
40 | k: !env.overwriteConfidently,
|
41 | C: program.cwd
|
42 | }))
|
43 | .on('close', () => {
|
44 | gauge.hide()
|
45 | resolve()
|
46 | })
|
47 | } else {
|
48 | reject(new Error(`Fail to download starter-kit-${starterKit} ${version}`))
|
49 | }
|
50 | }).on('error', reject)
|
51 | })
|
52 | if (!graphicKits.includes(starterKit)) {
|
53 | await Promise.all([
|
54 | fs.ensureDir(path.join(srcPath, './fonts')),
|
55 | fs.ensureDir(path.join(srcPath, './sounds')),
|
56 | fs.ensureDir(path.join(srcPath, './images')),
|
57 | fs.ensureDir(path.join(srcPath, './json'))
|
58 | ])
|
59 | } else {
|
60 | await Promise.all([fs.ensureDir(path.join(srcPath, '../dist'))])
|
61 | console.log(chalk.green('✅ Built dist folder '))
|
62 | }
|
63 | } catch (err) {
|
64 | if (!program.silent) console.log(chalk.red('⚠️ Blink Init was NOT successful!'))
|
65 | return Promise.reject(err)
|
66 | }
|
67 | }
|