1 | const path = require('path')
|
2 | const fs = require('fs-extra')
|
3 | const chalk = require('chalk')
|
4 | const toml = require('toml')
|
5 | const resolveVideos = require('./resolve-videos')
|
6 | const atReplacer = require('./animate-template-replacer')
|
7 | const atbReplacer = require('./animate-template-banners-replacer')
|
8 | const atmReplacer = require('./animate-template-mraid-replacer')
|
9 | const { promisify } = require('util')
|
10 | const lookup = promisify(require('ps-node').lookup)
|
11 | const os = require('os')
|
12 | const fsOpts = { encoding: 'utf8' }
|
13 | const childProcess = require('child_process')
|
14 |
|
15 | module.exports = async ({ program, env }) => {
|
16 | try {
|
17 | const blinkPath = path.join(program.cwd, 'blink.toml')
|
18 | const srcPath = path.join(program.cwd, 'src')
|
19 | const pkgPath = path.join(program.cwd, 'package.json')
|
20 | let pkgJson
|
21 | try {
|
22 | pkgJson = fs.readJsonSync(pkgPath)
|
23 | } catch (e) {
|
24 | console.log(chalk.red('⚠️ You may be in the wrong directory or haven\'t run \'blink init\''))
|
25 | await Promise.reject(e)
|
26 | }
|
27 |
|
28 | if (pkgJson.kitType !== undefined && pkgJson.kitType === 'graphics') {
|
29 | console.log(chalk.yellow('Nothing to prepare in a graphic kit'))
|
30 | return
|
31 | }
|
32 |
|
33 | const animateProcess = await lookup({
|
34 | command: 'Adobe',
|
35 | arguments: 'Animate',
|
36 | psargs: 'ux'
|
37 | })
|
38 | if (animateProcess.length && env.auto) {
|
39 | console.log(chalk.red('⚠️ Close Animate CC before running \'blink prepare --auto\''))
|
40 | return
|
41 | }
|
42 | const fileContents = await Promise.all([
|
43 | fs.readFile(blinkPath, fsOpts)
|
44 | .then((body) => toml.parse(body))
|
45 | .then((blink) => resolveVideos(blink))
|
46 | ])
|
47 | let templateName
|
48 | if (fileContents[0].general.adFormat === 'banner') {
|
49 | const animateTemplateBannersPath = path.join(srcPath, 'animate-template-banners.html')
|
50 | const animateTemplate = atbReplacer(fileContents[0], await fs.readFile(animateTemplateBannersPath, fsOpts), pkgJson)
|
51 | await fs.writeFile(animateTemplateBannersPath, animateTemplate, fsOpts)
|
52 | templateName = animateTemplateBannersPath
|
53 | } else if (fileContents[0].general.adFormat === 'mraid') {
|
54 | const animateTemplateMraidPath = path.join(srcPath, 'animate-template-mraid.html')
|
55 | const animateTemplate = atmReplacer(fileContents[0], await fs.readFile(animateTemplateMraidPath, fsOpts), pkgJson)
|
56 | await fs.writeFile(animateTemplateMraidPath, animateTemplate, fsOpts)
|
57 | templateName = animateTemplateMraidPath
|
58 | } else {
|
59 | const animateTemplatePath = path.join(srcPath, 'animate-template.html')
|
60 | const animateTemplate = atReplacer(fileContents[0], await fs.readFile(animateTemplatePath, fsOpts), pkgJson)
|
61 | await fs.writeFile(animateTemplatePath, animateTemplate, fsOpts)
|
62 | templateName = animateTemplatePath
|
63 | }
|
64 |
|
65 | let jsfl
|
66 | if (env.auto || env.import) {
|
67 |
|
68 | if (env.import) {
|
69 | if (fs.existsSync('src/import.jsfl')) {
|
70 | jsfl = 'import.jsfl'
|
71 | } else {
|
72 | console.log(chalk.red('⚠️ src directory is missing import.jsfl'))
|
73 | }
|
74 | } else {
|
75 | if (fs.existsSync('src/custom.jsfl')) {
|
76 | jsfl = 'custom.jsfl'
|
77 | } else if (fs.existsSync('src/default.jsfl')) {
|
78 | jsfl = 'default.jsfl'
|
79 | }
|
80 | }
|
81 | if (jsfl) {
|
82 | const msg = `ℹ️ Opening Animate CC and running ${jsfl}`
|
83 | console.log(msg)
|
84 | jsfl = 'src/' + jsfl
|
85 | let cmd
|
86 | if (os.platform() === 'win32') {
|
87 | cmd = `Animate.exe ${jsfl}`
|
88 | } else {
|
89 | cmd = `open ${jsfl}`
|
90 | }
|
91 | |
92 |
|
93 |
|
94 | try {
|
95 | const stdOut = childProcess.execSync(cmd)
|
96 | if (stdOut.toString()) {
|
97 | console.log(stdOut.toString())
|
98 | }
|
99 | } catch (ex) {}
|
100 | }
|
101 | }
|
102 | if (!jsfl && !program.silent) {
|
103 | console.log(`ℹ️ Updated ${templateName}`)
|
104 | console.log(chalk.green('ℹ️ Reimport the HTML Publish Template using Publish Settings... ➡️ Advanced ➡️ Import New...'))
|
105 | }
|
106 | } catch (err) {
|
107 | if (!program.silent) console.log(chalk.red('⚠️ Blink Prepare was NOT successful!'))
|
108 | return Promise.reject(err)
|
109 | }
|
110 | }
|