-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap-amd.js
executable file
·174 lines (137 loc) · 4.97 KB
/
bootstrap-amd.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
/**
* This file converts Twitter Bootstrap JS files to use define() syntax.
*
* It's just a changed jqueryui-amd (https://github.com/jrburke/jqueryui-amd)
*
* It should be run in node (only 0.6+).
*/
/*global */
'use strict';
var fs = require( 'fs' ),
path = require( 'path' ),
inDir = process.argv[ 2 ],
optionKey = process.argv[ 3 ],
jsFileRegExp = /\.js$/,
exists = fs.existsSync || path.existsSync,
bootstrapSrcDir,
bootstrapPaths,
popoverContent,
allPlugins = [];
//Make sure required fields are present.
if ( !inDir ) {
console.log( 'Usage: bootstrap-amd bootstrapSrcDir' );
console.log( 'git clone https://github.com/twitter/bootstrap.git' );
process.exit( 1 );
};
//Normalize directory
inDir = path.normalize( inDir );
if ( inDir.lastIndexOf( '/' ) !== inDir.length - 1 ) {
inDir += '/';
};
//Make sure there is a js directory in there, otherwise cannot
//convert correctly.
bootstrapSrcDir = path.join( inDir, 'js/' );
if ( !exists(bootstrapSrcDir) || !fs.statSync(bootstrapSrcDir).isDirectory() ) {
console.log( 'The directory does not appear to contain Twitter Bootstrap, ' +
'not converting any files. Looking for "js" directory ' +
'in the source directory failed.' );
process.exit( 1 );
};
//For each file that is a sibling to bootstrap, transform to define.
bootstrapPaths = fs.readdirSync( bootstrapSrcDir );
bootstrapPaths.forEach(function( fileName ) {
var srcPath = bootstrapSrcDir + fileName;
if ( fs.statSync(srcPath).isFile() && jsFileRegExp.test(srcPath) ) {
var content = fs.readFileSync(srcPath, 'utf8');
convert( fileName, replacejQuery(content) );
// ignore popover because it has dependencies,
// thus it should be at the end of the file
if ( !/popover/.test( fileName ) ) {
allPlugins.push( replacejQuery(content) );
} else {
popoverContent = replacejQuery(content);
};
};
});
// now we can add popover
allPlugins.push( popoverContent );
createBootstrapAll();
createPackageJson();
console.log( 'Done. See \'' + path.join(inDir, 'amd') + '\' for the AMD version of the Bootstrap and \'' + path.join(inDir, 'amd/src') + '\' for AMD modules.' );
/* functions */
function mkDir(dir) {
if ( !exists(dir) ) {
//511 is decimal for 0777 octal
fs.mkdirSync( dir, 511 );
};
};
function replacejQuery( content ) {
return content.replace( /window\.jQuery/gim, 'jQuery' );
}
function createBootstrapAll() {
var content = allPlugins.join( '' ),
content = 'define(' +
'[ \'jquery\' ], function ( jQuery ) {\n' +
content +
'\n});';
fs.writeFile( path.normalize(inDir + '/amd/main.js'), content );
};
function createPackageJson() {
var packageJson = fs.readFileSync( inDir + 'package.json', 'utf8' );
packageJson = JSON.parse( packageJson );
packageJson.name = 'bootstrap-amd';
packageJson.description = 'AMD version of Twitter Bootstrap JavaScript modules. Converted with bootstrap-amd npm package.';
packageJson.keywords = [ 'bootstrap', 'twitter', 'modules', 'ready to use', 'modals', 'affix', 'tooltips', 'collapse', 'dropdowns', 'popovers', 'carousel', 'scrollspy', 'alert messages', 'typeahead', 'togglable tabs', 'buttons', 'transitions' ];
packageJson.dependencies = { 'jquery': '>1.5.0' };
delete packageJson.scripts;
delete packageJson.devDependencies;
packageJson = JSON.stringify( packageJson, null, ' ' );
fs.writeFile( path.normalize(inDir + '/amd/package.json'), packageJson );
};
/**
* Converts the contents of a file to an AMD module.
* @param {String} contents the file contents.
*/
function convert( fileName, contents ) {
var outFileName,
fileParts = fileName.split( '-' ),
tempDir,
files;
//Remove 'bootstrap' prefix from the files,
//generate module name.
if ( fileParts[0].indexOf( 'bootstrap' ) !== -1 ) {
outFileName = fileParts[ 1 ];
} else {
outFileName = fileName;
};
mkDir( inDir + 'amd/' );
tempDir = inDir + 'amd/src/';
mkDir( tempDir );
files = getDependencies( outFileName );
contents = 'define(' +
'[ ' + files.join(',') + ' ], function ( jQuery ) {\n' +
contents +
'\n});';
fs.writeFileSync( tempDir + outFileName, contents );
};
function getDependencies( fileName ) {
var deps;
if ( optionKey !== '--no-transition' ) {
switch ( fileName ) {
case 'transition.js':
deps = [ "'jquery'" ];
break;
case 'popover.js':
deps = [ "'jquery', './tooltip', './transition'" ];
break;
default:
deps = [ "'jquery', './transition'" ];
break;
};
} else {
fileName === 'popover.js' ?
deps = [ "'jquery', './tooltip'" ] : deps = [ "'jquery'" ];
};
return deps;
};