UNPKG

4.45 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3// Import packages:
4const fs = require('fs-extra')
5const replace = require('replace-in-file')
6const argv = require('yargs').argv
7const createPackage = require('../resources/createPackage')
8
9// Import local values:
10const p = require("path")
11const homedir = (process.platform === "win32") ? process.env.HOMEPATH : process.env.HOME
12const user = (process.platform === "win32") ? process.env.USERNAME : process.env.USER
13const pkg = require('../package.json')
14
15const composi = (() => {
16 /**
17 * Create variables based on command line arguments.
18 */
19 const originalName = argv.n
20 const name = argv.n ? argv.n.toLowerCase() : ''
21 let path = argv.path || argv.p || p.join(homedir, 'Desktop')
22 const version = argv.version || argv.v
23 const composi_path = __dirname.split(p.sep + 'bin')[0]
24 const packageName = name.replace(' ', '-')
25 const package = createPackage({name: packageName, user: user, version: pkg.version})
26
27 const deploy = argv.d || argv.deploy
28
29 if (version) {
30 return console.log(pkg.version)
31 }
32 if (!name) {
33 if (deploy) {
34 console.log('Deploying to production.')
35 console.log('Please wait.')
36
37 let deployName = process.cwd().split(p.sep)
38 deployName = deployName[deployName.length -1]
39 deployName = `${deployName}-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 // Create new project:
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})()