1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | const fs = require('fs-extra')
|
5 | const replace = require('replace-in-file')
|
6 | const argv = require('yargs').argv
|
7 | const createPackage = require('../resources/createPackage')
|
8 |
|
9 |
|
10 | const p = require("path")
|
11 | const homedir = (process.platform === "win32") ? process.env.HOMEPATH : process.env.HOME
|
12 | const user = (process.platform === "win32") ? process.env.USERNAME : process.env.USER
|
13 |
|
14 | const pkg = require('../package.json')
|
15 |
|
16 | const composi = (() => {
|
17 | |
18 |
|
19 |
|
20 | const originalName = argv.n
|
21 | const name = argv.n ? argv.n.toLowerCase() : ''
|
22 | let path = argv.path || argv.p || p.join(homedir, 'Desktop')
|
23 | const version = argv.version || argv.v
|
24 | const composi_path = __dirname.split(p.sep + 'bin')[0]
|
25 | const packageName = name.replace(' ', '-')
|
26 | const _package = createPackage({ name: packageName, user: user, version: pkg.version})
|
27 |
|
28 | const deploy = argv.d || argv.deploy
|
29 |
|
30 | if (version) {
|
31 | return console.log(pkg.version)
|
32 | }
|
33 | if (!name) {
|
34 | if (deploy) {
|
35 | console.log('Deploying to production.')
|
36 | console.log('Please wait.')
|
37 |
|
38 | let splitName = process.cwd().split(p.sep)
|
39 | let deployName = `${splitName[splitName.length -1]}-production`
|
40 | fs.copy(p.join(process.cwd(), 'index.html'), p.join(path, deployName, 'index.html'))
|
41 | fs.copy(p.join(process.cwd(), 'js'), p.join(path, deployName, 'js'))
|
42 | fs.copy(p.join(process.cwd(), 'css'), p.join(path, deployName, 'css'))
|
43 | fs.copy(p.join(process.cwd(), 'icons'), p.join(path, deployName, 'icons'))
|
44 | .catch(err => console.log('No icons to copy.'))
|
45 | fs.copy(p.join(process.cwd(), 'images'), p.join(path, deployName, 'images'))
|
46 | .catch(err => console.log('No images to copy.'))
|
47 | console.log('Deployment completed.')
|
48 | console.log('Project deployed at: ' + path + p.sep + deployName)
|
49 |
|
50 | } else {
|
51 | console.log('No name was provided. Please try again and provide a name.')
|
52 | console.log('Make sure you use the `-n` flag before the name.')
|
53 | console.log('Example: composi -n MyProject')
|
54 | }
|
55 | return
|
56 | }
|
57 | if (name) {
|
58 | path = p.join(path, originalName)
|
59 |
|
60 | console.log('Creating a new Composi project.')
|
61 | const packageName = name.replace(' ', '-')
|
62 | fs.copy(p.join(composi_path, 'resources', '.babelrc'), p.join(path, '.babelrc'))
|
63 | fs.copy(p.join(composi_path, 'resources', 'gulpfile.js'), p.join(path, 'gulpfile.js'))
|
64 | fs.copy(p.join(composi_path, 'resources', '.editorconfig'), p.join(path, '.editorconfig'))
|
65 | fs.copy(p.join(composi_path, 'resources', 'jsconfig.json'), p.join(path, 'jsconfig.json'))
|
66 | fs.copy(p.join(composi_path, 'resources', 'dev', 'css', 'styles.css'), p.join(path, 'dev','css', 'styles.css'))
|
67 | fs.copy(p.join(composi_path, 'resources', 'dev', 'app.js'), p.join(path, 'dev', 'app.js'))
|
68 | fs.copy(p.join(composi_path, 'resources', 'dev', 'title.js'), p.join(path, 'dev', 'components', 'title.js'))
|
69 | fs.copy(p.join(composi_path, 'resources', 'index.html'), p.join(path, 'index.html'))
|
70 | fs.outputFile(p.join(path, 'package.json'), _package)
|
71 | fs.copy(p.join(composi_path, 'resources', 'README.md'), p.join(path, 'README.md'))
|
72 | fs.copy(p.join(composi_path, 'resources', 'favicon-16x16.png'), p.join(path, 'images', 'favicon-16x16.png'))
|
73 | fs.copy(p.join(composi_path, 'resources', 'favicon-32x32.png'), p.join(path, 'images', 'favicon-32x32.png'))
|
74 | setTimeout(function() {
|
75 | replace({
|
76 | from: /project_name/g,
|
77 | to: originalName,
|
78 | files: [
|
79 | p.join(path, 'index.html')
|
80 | ],
|
81 | }),
|
82 | replace({
|
83 | from: /project_name/g,
|
84 | to: originalName,
|
85 | files: [
|
86 | p.join(path, 'README.md')
|
87 | ]
|
88 | }),
|
89 | setTimeout(function() {
|
90 | replace({
|
91 | from: /userName/g,
|
92 | to: user,
|
93 | files: [
|
94 | p.join(path, 'README.md')
|
95 | ]
|
96 | })}, 200),
|
97 | setTimeout(function() {
|
98 | replace({
|
99 | from: /currentYear/g,
|
100 | to: new Date().getFullYear(),
|
101 | files: [
|
102 | p.join(path, 'README.md')
|
103 | ]
|
104 | })}, 400),
|
105 | console.log('The project has been created.')
|
106 | console.log('Use the terminal to cd to it and run `npm i` to install its dependencies.')
|
107 | console.log('After the install is done, you can run `gulp` to build and run your project.')
|
108 | }, 2500)
|
109 | }
|
110 | })()
|