"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/init.js" (8 Mar 2017, 10707 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 (function() {
    2   var Command, Init, fs, path, yargs,
    3     extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
    4     hasProp = {}.hasOwnProperty,
    5     indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
    6 
    7   path = require('path');
    8 
    9   yargs = require('yargs');
   10 
   11   Command = require('./command');
   12 
   13   fs = require('./fs');
   14 
   15   module.exports = Init = (function(superClass) {
   16     extend(Init, superClass);
   17 
   18     function Init() {
   19       return Init.__super__.constructor.apply(this, arguments);
   20     }
   21 
   22     Init.commandNames = ['init'];
   23 
   24     Init.prototype.supportedSyntaxes = ['coffeescript', 'javascript'];
   25 
   26     Init.prototype.parseOptions = function(argv) {
   27       var options;
   28       options = yargs(argv).wrap(100);
   29       options.usage("Usage:\n  apm init -p <package-name>\n  apm init -p <package-name> --syntax <javascript-or-coffeescript>\n  apm init -p <package-name> -c ~/Downloads/r.tmbundle\n  apm init -p <package-name> -c https://github.com/textmate/r.tmbundle\n  apm init -p <package-name> --template /path/to/your/package/template\n\n  apm init -t <theme-name>\n  apm init -t <theme-name> -c ~/Downloads/Dawn.tmTheme\n  apm init -t <theme-name> -c https://raw.github.com/chriskempson/tomorrow-theme/master/textmate/Tomorrow-Night-Eighties.tmTheme\n  apm init -t <theme-name> --template /path/to/your/theme/template\n\n  apm init -l <language-name>\n\nGenerates code scaffolding for either a theme or package depending\non the option selected.");
   30       options.alias('p', 'package').string('package').describe('package', 'Generates a basic package');
   31       options.alias('s', 'syntax').string('syntax').describe('syntax', 'Sets package syntax to CoffeeScript or JavaScript');
   32       options.alias('t', 'theme').string('theme').describe('theme', 'Generates a basic theme');
   33       options.alias('l', 'language').string('language').describe('language', 'Generates a basic language package');
   34       options.alias('c', 'convert').string('convert').describe('convert', 'Path or URL to TextMate bundle/theme to convert');
   35       options.alias('h', 'help').describe('help', 'Print this usage message');
   36       return options.string('template').describe('template', 'Path to the package or theme template');
   37     };
   38 
   39     Init.prototype.run = function(options) {
   40       var callback, languageName, languagePath, packagePath, ref, ref1, ref2, syntax, templatePath, themePath;
   41       callback = options.callback;
   42       options = this.parseOptions(options.commandArgs);
   43       if (((ref = options.argv["package"]) != null ? ref.length : void 0) > 0) {
   44         if (options.argv.convert) {
   45           return this.convertPackage(options.argv.convert, options.argv["package"], callback);
   46         } else {
   47           packagePath = path.resolve(options.argv["package"]);
   48           syntax = options.argv.syntax || this.supportedSyntaxes[0];
   49           if (indexOf.call(this.supportedSyntaxes, syntax) < 0) {
   50             return callback("You must specify one of " + (this.supportedSyntaxes.join(', ')) + " after the --syntax argument");
   51           }
   52           templatePath = this.getTemplatePath(options.argv, "package-" + syntax);
   53           this.generateFromTemplate(packagePath, templatePath);
   54           return callback();
   55         }
   56       } else if (((ref1 = options.argv.theme) != null ? ref1.length : void 0) > 0) {
   57         if (options.argv.convert) {
   58           return this.convertTheme(options.argv.convert, options.argv.theme, callback);
   59         } else {
   60           themePath = path.resolve(options.argv.theme);
   61           templatePath = this.getTemplatePath(options.argv, 'theme');
   62           this.generateFromTemplate(themePath, templatePath);
   63           return callback();
   64         }
   65       } else if (((ref2 = options.argv.language) != null ? ref2.length : void 0) > 0) {
   66         languagePath = path.resolve(options.argv.language);
   67         languageName = path.basename(languagePath).replace(/^language-/, '');
   68         languagePath = path.join(path.dirname(languagePath), "language-" + languageName);
   69         templatePath = this.getTemplatePath(options.argv, 'language');
   70         this.generateFromTemplate(languagePath, templatePath, languageName);
   71         return callback();
   72       } else if (options.argv["package"] != null) {
   73         return callback('You must specify a path after the --package argument');
   74       } else if (options.argv.theme != null) {
   75         return callback('You must specify a path after the --theme argument');
   76       } else {
   77         return callback('You must specify either --package, --theme or --language to `apm init`');
   78       }
   79     };
   80 
   81     Init.prototype.convertPackage = function(sourcePath, destinationPath, callback) {
   82       var PackageConverter, converter;
   83       if (!destinationPath) {
   84         callback("Specify directory to create package in using --package");
   85         return;
   86       }
   87       PackageConverter = require('./package-converter');
   88       converter = new PackageConverter(sourcePath, destinationPath);
   89       return converter.convert((function(_this) {
   90         return function(error) {
   91           var templatePath;
   92           if (error != null) {
   93             return callback(error);
   94           } else {
   95             destinationPath = path.resolve(destinationPath);
   96             templatePath = path.resolve(__dirname, '..', 'templates', 'bundle');
   97             _this.generateFromTemplate(destinationPath, templatePath);
   98             return callback();
   99           }
  100         };
  101       })(this));
  102     };
  103 
  104     Init.prototype.convertTheme = function(sourcePath, destinationPath, callback) {
  105       var ThemeConverter, converter;
  106       if (!destinationPath) {
  107         callback("Specify directory to create theme in using --theme");
  108         return;
  109       }
  110       ThemeConverter = require('./theme-converter');
  111       converter = new ThemeConverter(sourcePath, destinationPath);
  112       return converter.convert((function(_this) {
  113         return function(error) {
  114           var templatePath;
  115           if (error != null) {
  116             return callback(error);
  117           } else {
  118             destinationPath = path.resolve(destinationPath);
  119             templatePath = path.resolve(__dirname, '..', 'templates', 'theme');
  120             _this.generateFromTemplate(destinationPath, templatePath);
  121             fs.removeSync(path.join(destinationPath, 'styles', 'colors.less'));
  122             fs.removeSync(path.join(destinationPath, 'LICENSE.md'));
  123             return callback();
  124           }
  125         };
  126       })(this));
  127     };
  128 
  129     Init.prototype.generateFromTemplate = function(packagePath, templatePath, packageName) {
  130       var childPath, contents, i, len, packageAuthor, ref, relativePath, results, sourcePath, templateChildPath;
  131       if (packageName == null) {
  132         packageName = path.basename(packagePath);
  133       }
  134       packageAuthor = process.env.GITHUB_USER || 'atom';
  135       fs.makeTreeSync(packagePath);
  136       ref = fs.listRecursive(templatePath);
  137       results = [];
  138       for (i = 0, len = ref.length; i < len; i++) {
  139         childPath = ref[i];
  140         templateChildPath = path.resolve(templatePath, childPath);
  141         relativePath = templateChildPath.replace(templatePath, "");
  142         relativePath = relativePath.replace(/^\//, '');
  143         relativePath = relativePath.replace(/\.template$/, '');
  144         relativePath = this.replacePackageNamePlaceholders(relativePath, packageName);
  145         sourcePath = path.join(packagePath, relativePath);
  146         if (fs.existsSync(sourcePath)) {
  147           continue;
  148         }
  149         if (fs.isDirectorySync(templateChildPath)) {
  150           results.push(fs.makeTreeSync(sourcePath));
  151         } else if (fs.isFileSync(templateChildPath)) {
  152           fs.makeTreeSync(path.dirname(sourcePath));
  153           contents = fs.readFileSync(templateChildPath).toString();
  154           contents = this.replacePackageNamePlaceholders(contents, packageName);
  155           contents = this.replacePackageAuthorPlaceholders(contents, packageAuthor);
  156           contents = this.replaceCurrentYearPlaceholders(contents);
  157           results.push(fs.writeFileSync(sourcePath, contents));
  158         } else {
  159           results.push(void 0);
  160         }
  161       }
  162       return results;
  163     };
  164 
  165     Init.prototype.replacePackageAuthorPlaceholders = function(string, packageAuthor) {
  166       return string.replace(/__package-author__/g, packageAuthor);
  167     };
  168 
  169     Init.prototype.replacePackageNamePlaceholders = function(string, packageName) {
  170       var placeholderRegex;
  171       placeholderRegex = /__(?:(package-name)|([pP]ackageName)|(package_name))__/g;
  172       return string = string.replace(placeholderRegex, (function(_this) {
  173         return function(match, dash, camel, underscore) {
  174           if (dash) {
  175             return _this.dasherize(packageName);
  176           } else if (camel) {
  177             if (/[a-z]/.test(camel[0])) {
  178               packageName = packageName[0].toLowerCase() + packageName.slice(1);
  179             } else if (/[A-Z]/.test(camel[0])) {
  180               packageName = packageName[0].toUpperCase() + packageName.slice(1);
  181             }
  182             return _this.camelize(packageName);
  183           } else if (underscore) {
  184             return _this.underscore(packageName);
  185           }
  186         };
  187       })(this));
  188     };
  189 
  190     Init.prototype.replaceCurrentYearPlaceholders = function(string) {
  191       return string.replace('__current_year__', new Date().getFullYear());
  192     };
  193 
  194     Init.prototype.getTemplatePath = function(argv, templateType) {
  195       if (argv.template != null) {
  196         return path.resolve(argv.template);
  197       } else {
  198         return path.resolve(__dirname, '..', 'templates', templateType);
  199       }
  200     };
  201 
  202     Init.prototype.dasherize = function(string) {
  203       string = string[0].toLowerCase() + string.slice(1);
  204       return string.replace(/([A-Z])|(_)/g, function(m, letter, underscore) {
  205         if (letter) {
  206           return "-" + letter.toLowerCase();
  207         } else {
  208           return "-";
  209         }
  210       });
  211     };
  212 
  213     Init.prototype.camelize = function(string) {
  214       return string.replace(/[_-]+(\w)/g, function(m) {
  215         return m[1].toUpperCase();
  216       });
  217     };
  218 
  219     Init.prototype.underscore = function(string) {
  220       string = string[0].toLowerCase() + string.slice(1);
  221       return string.replace(/([A-Z])|(-)/g, function(m, letter, dash) {
  222         if (letter) {
  223           return "_" + letter.toLowerCase();
  224         } else {
  225           return "_";
  226         }
  227       });
  228     };
  229 
  230     return Init;
  231 
  232   })(Command);
  233 
  234 }).call(this);