UNPKG

5.43 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
7
8// Import local values:
9const p = require("path")
10const homedir = (process.platform === "win32") ? process.env.HOMEPATH : process.env.HOME
11const user = (process.platform === "win32") ? process.env.USERNAME : process.env.USER
12const pkg = require('../package.json')
13
14function createPackage(opts) {
15 return `{
16 "name": "${opts.name}",
17 "version": "1.0.0",
18 "description": "Put your description here.",
19 "main": "app.js",
20 "scripts": {
21 "build": "gulp",
22 "start": "npm run build",
23 "test": "echo \\"Error: no test specified\\" && exit 1",
24 "prepare": "npm run build"
25 },
26 "author": "${opts.user}",
27 "repository": "foo",
28 "license": "MIT",
29 "devDependencies": {
30 "babel-cli": "^6.26.0",
31 "babel-plugin-external-helpers": "^6.22.0",
32 "babel-plugin-transform-class-properties": "^6.24.1",
33 "babel-plugin-transform-react-jsx": "^6.24.1",
34 "babel-preset-env": "^1.6.0",
35 "bluebird": "~2.9.24",
36 "browser-sync": "^2.12.10",
37 "composi": "^${opts.version}",
38 "gulp": "^3.9.1",
39 "gulp-better-rollup": "^1.1.1",
40 "gulp-cssnano": "^2.1.2",
41 "gulp-gzip": "^1.4.0",
42 "gulp-sourcemaps": "^2.6.1",
43 "rollup-plugin-babel": "^2.7.1",
44 "rollup-plugin-commonjs": "^8.0.2",
45 "rollup-plugin-node-resolve": "^3.0.0",
46 "rollup-plugin-uglify": "^2.0.1"
47 }
48}
49`
50}
51const composi = (() => {
52 /**
53 * Create variables based on command line arguments.
54 */
55 const originalName = argv.n
56 const name = argv.n ? argv.n.toLowerCase() : ''
57 let path = argv.path || argv.p || p.join(homedir, 'Desktop')
58 const version = argv.version || argv.v
59 const composi_path = __dirname.split(p.sep + 'bin')[0]
60 const packageName = name.replace(' ', '-')
61 const package = createPackage({name: packageName, user: user, version: pkg.version})
62
63 const deploy = argv.d || argv.deploy
64
65 if (version) {
66 return console.log(pkg.version)
67 }
68 if (!name) {
69 if (deploy) {
70 console.log('Deploying to production.')
71 console.log('Please wait.')
72
73 let deployName = process.cwd().split(p.sep)
74 deployName = deployName[deployName.length -1]
75 deployName = `${deployName}-production`
76 fs.copy(p.join(process.cwd(), 'index.html'), p.join(path, deployName, 'index.html'))
77 fs.copy(p.join(process.cwd(), 'js'), p.join(path, deployName, 'js'))
78 fs.copy(p.join(process.cwd(), 'css'), p.join(path, deployName, 'css'))
79 fs.copy(p.join(process.cwd(), 'icons'), p.join(path, deployName, 'icons'))
80 .catch(err => console.log('No icons to copy.'))
81 fs.copy(p.join(process.cwd(), 'images'), p.join(path, deployName, 'images'))
82 .catch(err => console.log('No images to copy.'))
83 console.log('Deployment completed.')
84 console.log('Project deployed at: ' + path + p.sep + deployName)
85
86 } else {
87 console.log('No name was provided. Please try again and provide a name.')
88 console.log('Make sure you use the `-n` flag before the name.')
89 console.log('Example: composi -n MyProject')
90 }
91 return
92 }
93 if (name) {
94 path = p.join(path, originalName)
95 // Create new project:
96 console.log('Creating a new Composi project.')
97 const packageName = name.replace(' ', '-')
98 fs.copy(p.join(composi_path, 'resources', '.babelrc'), p.join(path, '.babelrc'))
99 fs.copy(p.join(composi_path, 'resources', 'gulpfile.js'), p.join(path, 'gulpfile.js'))
100 fs.copy(p.join(composi_path, 'resources', '.editorconfig'), p.join(path, '.editorconfig'))
101 fs.copy(p.join(composi_path, 'resources', 'jsconfig.json'), p.join(path, 'jsconfig.json'))
102 fs.copy(p.join(composi_path, 'resources', 'dev', 'css', 'styles.css'), p.join(path, 'dev','css', 'styles.css'))
103 fs.copy(p.join(composi_path, 'resources', 'dev', 'app.js'), p.join(path, 'dev', 'app.js'))
104 fs.copy(p.join(composi_path, 'resources', 'dev', 'title.js'), p.join(path, 'dev', 'components', 'title.js'))
105 fs.copy(p.join(composi_path, 'resources', 'index.html'), p.join(path, 'index.html'))
106 fs.outputFile(p.join(path, 'package.json'), package)
107 fs.copy(p.join(composi_path, 'resources', 'README.md'), p.join(path, 'README.md'))
108 fs.copy(p.join(composi_path, 'resources', 'favicon-16x16.png'), p.join(path, 'images', 'favicon-16x16.png'))
109 fs.copy(p.join(composi_path, 'resources', 'favicon-32x32.png'), p.join(path, 'images', 'favicon-32x32.png'))
110 setTimeout(function() {
111 replace({
112 from: /project_name/g,
113 to: originalName,
114 files: [
115 p.join(path, 'index.html')
116 ],
117 }),
118 replace({
119 from: /project_name/g,
120 to: originalName,
121 files: [
122 p.join(path, 'README.md')
123 ]
124 }),
125 setTimeout(function() {
126 replace({
127 from: /userName/g,
128 to: user,
129 files: [
130 p.join(path, 'README.md')
131 ]
132 })}, 200),
133 setTimeout(function() {
134 replace({
135 from: /currentYear/g,
136 to: new Date().getFullYear(),
137 files: [
138 p.join(path, 'README.md')
139 ]
140 })}, 400),
141 console.log('The project has been created.')
142 console.log('Use the terminal to cd to it and run `npm i` to install its dependencies.')
143 console.log('After the install is done, you can run `gulp` to build and run your project.')
144 }, 2500)
145 }
146})()