This repository was archived by the owner on Mar 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscaffold.js
executable file
·145 lines (126 loc) · 3.75 KB
/
scaffold.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
// Include modules
var fs = require('fs'),
path = require('path'),
prompt = require('prompt'),
base = path.resolve(__dirname, '..', '..', '..'),
bowerFile = path.join(base, 'bower.json'),
packageFile = path.join(base, 'package.json'),
projectFile = path.join(base, 'project.json');
// The root project folder
var scaffoldBase = path.join(__dirname, '..', 'scaffold');
/**
* Create a directory if it doesn't exist.
* We use the synchronous API because async was failing after a certain # of folders
* @method scaffoldDir
* @param {String} dir The directory path to create
*/
function scaffoldDir(dir)
{
var target = path.join(base, dir);
if (!fs.existsSync(target))
{
fs.mkdirSync(target);
console.log(" " + dir + " ... added");
}
}
/**
* Create a file if it doesn't exist
* @method create
* @param {String} file The file path
* @param {String|Object} content The default content for file
* @param {function} callback The callback function when done
*/
function scaffold(file, content, callback)
{
var source = path.join(scaffoldBase, file);
var target = path.join(base, file);
if (!fs.existsSync(target))
{
if (!content && !fs.existsSync(source))
{
throw "Source file doesn't exist '" + source + "'";
}
fs.writeFileSync(target, content || fs.readFileSync(source));
console.log(" " + file + " ... added");
if (callback) callback(target);
}
}
/**
* Do a default grunt build
* @method gruntDefault
*/
function gruntRun()
{
var isWindows = process.platform === 'win32';
var cwd = path.dirname(projectFile);
var spawn = require('child_process').spawn;
var grunt = isWindows ?
spawn(process.env.comspec, ['/c', 'grunt'], { cwd: cwd }):
spawn('grunt', [], { cwd: cwd });
grunt.stdout.on('data', function (data) {
process.stdout.write(data);
});
grunt.stderr.on('data', function (data) {
process.stdout.write(data);
});
grunt.on('error', function(err){
console.log("Grunt run error", err);
});
}
// Only scaffold the project if no Gruntfile is available
scaffold("Gruntfile.js", null, function(file){
// Create the required folders
scaffoldDir("src");
scaffoldDir("deploy");
scaffoldDir("deploy/assets");
scaffoldDir("deploy/assets/css");
scaffoldDir("deploy/assets/js");
// Copy the required files
scaffold("project.json");
scaffold("deploy/index.html");
scaffold("README.md");
scaffold(".bowerrc");
scaffold("package.json");
scaffold("bower.json");
scaffold("src/main.js");
scaffold("src/main.less");
scaffold(".gitignore", "node_modules\ncomponents");
prompt.start();
prompt.get([{
name : 'name',
description: 'The human-readable name of the project',
pattern: /^[a-zA-Z\-0-9]+?$/,
message: "Name can only contain letters, numbers and hyphens",
required: true
}, {
name : 'version',
description: 'The starting version of the project',
default: '0.0.1',
pattern: /^\d+\.\d+(\.\d+)?$/,
message: "Version must be in the format #.#.#",
required: true
}
], function(err, result){
if (!err)
{
// Get the build file as an object
var project = JSON.parse(fs.readFileSync(projectFile));
project.name = result.name;
project.version = result.version;
// Update the project file with the new name
fs.writeFileSync(projectFile, JSON.stringify(project, null, "\t"));
// Get the project file as an object
var bower = JSON.parse(fs.readFileSync(bowerFile));
bower.name = result.name;
bower.version = result.version;
// Update the project file with the new name
fs.writeFileSync(bowerFile, JSON.stringify(bower, null, "\t"));
var pack = JSON.parse(fs.readFileSync(packageFile));
pack.version = result.version;
// Update the project file with the new name
fs.writeFileSync(packageFile, JSON.stringify(pack, null, "\t"));
}
gruntRun();
});
});