"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/lib/package-converter.js" (8 Mar 2017, 11243 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 CSON, PackageConverter, ScopeSelector, _, fs, path, plist, request, tar, temp, url, zlib;
    3 
    4   path = require('path');
    5 
    6   url = require('url');
    7 
    8   zlib = require('zlib');
    9 
   10   _ = require('underscore-plus');
   11 
   12   CSON = require('season');
   13 
   14   plist = require('plist');
   15 
   16   ScopeSelector = require('first-mate').ScopeSelector;
   17 
   18   tar = require('tar');
   19 
   20   temp = require('temp');
   21 
   22   fs = require('./fs');
   23 
   24   request = require('./request');
   25 
   26   module.exports = PackageConverter = (function() {
   27     function PackageConverter(sourcePath1, destinationPath) {
   28       this.sourcePath = sourcePath1;
   29       this.destinationPath = path.resolve(destinationPath);
   30       this.plistExtensions = ['.plist', '.tmCommand', '.tmLanguage', '.tmMacro', '.tmPreferences', '.tmSnippet'];
   31       this.directoryMappings = {
   32         'Preferences': 'settings',
   33         'Snippets': 'snippets',
   34         'Syntaxes': 'grammars'
   35       };
   36     }
   37 
   38     PackageConverter.prototype.convert = function(callback) {
   39       var protocol;
   40       protocol = url.parse(this.sourcePath).protocol;
   41       if (protocol === 'http:' || protocol === 'https:') {
   42         return this.downloadBundle(callback);
   43       } else {
   44         return this.copyDirectories(this.sourcePath, callback);
   45       }
   46     };
   47 
   48     PackageConverter.prototype.getDownloadUrl = function() {
   49       var downloadUrl;
   50       downloadUrl = this.sourcePath;
   51       downloadUrl = downloadUrl.replace(/(\.git)?\/*$/, '');
   52       return downloadUrl += '/archive/master.tar.gz';
   53     };
   54 
   55     PackageConverter.prototype.downloadBundle = function(callback) {
   56       var requestOptions, tempPath;
   57       tempPath = temp.mkdirSync('atom-bundle-');
   58       requestOptions = {
   59         url: this.getDownloadUrl()
   60       };
   61       return request.createReadStream(requestOptions, (function(_this) {
   62         return function(readStream) {
   63           readStream.on('response', function(arg) {
   64             var headers, statusCode;
   65             headers = arg.headers, statusCode = arg.statusCode;
   66             if (statusCode !== 200) {
   67               return callback("Download failed (" + headers.status + ")");
   68             }
   69           });
   70           return readStream.pipe(zlib.createGunzip()).pipe(tar.Extract({
   71             path: tempPath
   72           })).on('error', function(error) {
   73             return callback(error);
   74           }).on('end', function() {
   75             var sourcePath;
   76             sourcePath = path.join(tempPath, fs.readdirSync(tempPath)[0]);
   77             return _this.copyDirectories(sourcePath, callback);
   78           });
   79         };
   80       })(this));
   81     };
   82 
   83     PackageConverter.prototype.copyDirectories = function(sourcePath, callback) {
   84       var packageName, ref;
   85       sourcePath = path.resolve(sourcePath);
   86       try {
   87         packageName = (ref = JSON.parse(fs.readFileSync(path.join(sourcePath, 'package.json')))) != null ? ref.packageName : void 0;
   88       } catch (undefined) {}
   89       if (packageName == null) {
   90         packageName = path.basename(this.destinationPath);
   91       }
   92       this.convertSnippets(packageName, sourcePath);
   93       this.convertPreferences(packageName, sourcePath);
   94       this.convertGrammars(sourcePath);
   95       return callback();
   96     };
   97 
   98     PackageConverter.prototype.filterObject = function(object) {
   99       delete object.uuid;
  100       return delete object.keyEquivalent;
  101     };
  102 
  103     PackageConverter.prototype.convertSettings = function(settings) {
  104       var editorProperties, i, len, name, ref, ref1, shellVariables, value;
  105       if (settings.shellVariables) {
  106         shellVariables = {};
  107         ref = settings.shellVariables;
  108         for (i = 0, len = ref.length; i < len; i++) {
  109           ref1 = ref[i], name = ref1.name, value = ref1.value;
  110           shellVariables[name] = value;
  111         }
  112         settings.shellVariables = shellVariables;
  113       }
  114       editorProperties = _.compactObject({
  115         commentStart: _.valueForKeyPath(settings, 'shellVariables.TM_COMMENT_START'),
  116         commentEnd: _.valueForKeyPath(settings, 'shellVariables.TM_COMMENT_END'),
  117         increaseIndentPattern: settings.increaseIndentPattern,
  118         decreaseIndentPattern: settings.decreaseIndentPattern,
  119         foldEndPattern: settings.foldingStopMarker,
  120         completions: settings.completions
  121       });
  122       if (!_.isEmpty(editorProperties)) {
  123         return {
  124           editor: editorProperties
  125         };
  126       }
  127     };
  128 
  129     PackageConverter.prototype.readFileSync = function(filePath) {
  130       if (_.contains(this.plistExtensions, path.extname(filePath))) {
  131         return plist.parseFileSync(filePath);
  132       } else if (_.contains(['.json', '.cson'], path.extname(filePath))) {
  133         return CSON.readFileSync(filePath);
  134       }
  135     };
  136 
  137     PackageConverter.prototype.writeFileSync = function(filePath, object) {
  138       if (object == null) {
  139         object = {};
  140       }
  141       this.filterObject(object);
  142       if (Object.keys(object).length > 0) {
  143         return CSON.writeFileSync(filePath, object);
  144       }
  145     };
  146 
  147     PackageConverter.prototype.convertFile = function(sourcePath, destinationDir) {
  148       var contents, destinationName, destinationPath, extension;
  149       extension = path.extname(sourcePath);
  150       destinationName = (path.basename(sourcePath, extension)) + ".cson";
  151       destinationName = destinationName.toLowerCase();
  152       destinationPath = path.join(destinationDir, destinationName);
  153       if (_.contains(this.plistExtensions, path.extname(sourcePath))) {
  154         contents = plist.parseFileSync(sourcePath);
  155       } else if (_.contains(['.json', '.cson'], path.extname(sourcePath))) {
  156         contents = CSON.readFileSync(sourcePath);
  157       }
  158       return this.writeFileSync(destinationPath, contents);
  159     };
  160 
  161     PackageConverter.prototype.normalizeFilenames = function(directoryPath) {
  162       var child, childPath, convertedFileName, convertedPath, extension, i, len, ref, results, suffix;
  163       if (!fs.isDirectorySync(directoryPath)) {
  164         return;
  165       }
  166       ref = fs.readdirSync(directoryPath);
  167       results = [];
  168       for (i = 0, len = ref.length; i < len; i++) {
  169         child = ref[i];
  170         childPath = path.join(directoryPath, child);
  171         convertedFileName = child.replace(/[|?*<>:"\\\/]+/g, '-');
  172         if (child === convertedFileName) {
  173           continue;
  174         }
  175         convertedFileName = convertedFileName.replace(/[\s-]+/g, '-');
  176         convertedPath = path.join(directoryPath, convertedFileName);
  177         suffix = 1;
  178         while (fs.existsSync(convertedPath) || fs.existsSync(convertedPath.toLowerCase())) {
  179           extension = path.extname(convertedFileName);
  180           convertedFileName = (path.basename(convertedFileName, extension)) + "-" + suffix + extension;
  181           convertedPath = path.join(directoryPath, convertedFileName);
  182           suffix++;
  183         }
  184         results.push(fs.renameSync(childPath, convertedPath));
  185       }
  186       return results;
  187     };
  188 
  189     PackageConverter.prototype.convertSnippets = function(packageName, source) {
  190       var child, content, destination, extension, i, len, name, ref, ref1, scope, selector, snippet, snippetsBySelector, sourceSnippets, tabTrigger;
  191       sourceSnippets = path.join(source, 'snippets');
  192       if (!fs.isDirectorySync(sourceSnippets)) {
  193         sourceSnippets = path.join(source, 'Snippets');
  194       }
  195       if (!fs.isDirectorySync(sourceSnippets)) {
  196         return;
  197       }
  198       snippetsBySelector = {};
  199       destination = path.join(this.destinationPath, 'snippets');
  200       ref = fs.readdirSync(sourceSnippets);
  201       for (i = 0, len = ref.length; i < len; i++) {
  202         child = ref[i];
  203         snippet = (ref1 = this.readFileSync(path.join(sourceSnippets, child))) != null ? ref1 : {};
  204         scope = snippet.scope, name = snippet.name, content = snippet.content, tabTrigger = snippet.tabTrigger;
  205         if (!(tabTrigger && content)) {
  206           continue;
  207         }
  208         content = content.replace(/\$\{TM_[A-Z_]+:([^}]+)}/g, '$1');
  209         content = content.replace(/\$\{(\d)+:\s*\$\{TM_[^}]+\s*\}\s*\}/g, '$$1');
  210         content = content.replace(/\\\$/g, '$');
  211         if (name == null) {
  212           extension = path.extname(child);
  213           name = path.basename(child, extension);
  214         }
  215         if (scope) {
  216           selector = new ScopeSelector(scope).toCssSelector();
  217         }
  218         if (selector == null) {
  219           selector = '*';
  220         }
  221         if (snippetsBySelector[selector] == null) {
  222           snippetsBySelector[selector] = {};
  223         }
  224         snippetsBySelector[selector][name] = {
  225           prefix: tabTrigger,
  226           body: content
  227         };
  228       }
  229       this.writeFileSync(path.join(destination, packageName + ".cson"), snippetsBySelector);
  230       return this.normalizeFilenames(destination);
  231     };
  232 
  233     PackageConverter.prototype.convertPreferences = function(packageName, source) {
  234       var child, destination, i, key, len, preferencesBySelector, properties, ref, ref1, ref2, scope, selector, settings, sourcePreferences, value;
  235       sourcePreferences = path.join(source, 'preferences');
  236       if (!fs.isDirectorySync(sourcePreferences)) {
  237         sourcePreferences = path.join(source, 'Preferences');
  238       }
  239       if (!fs.isDirectorySync(sourcePreferences)) {
  240         return;
  241       }
  242       preferencesBySelector = {};
  243       destination = path.join(this.destinationPath, 'settings');
  244       ref = fs.readdirSync(sourcePreferences);
  245       for (i = 0, len = ref.length; i < len; i++) {
  246         child = ref[i];
  247         ref2 = (ref1 = this.readFileSync(path.join(sourcePreferences, child))) != null ? ref1 : {}, scope = ref2.scope, settings = ref2.settings;
  248         if (!(scope && settings)) {
  249           continue;
  250         }
  251         if (properties = this.convertSettings(settings)) {
  252           selector = new ScopeSelector(scope).toCssSelector();
  253           for (key in properties) {
  254             value = properties[key];
  255             if (preferencesBySelector[selector] == null) {
  256               preferencesBySelector[selector] = {};
  257             }
  258             if (preferencesBySelector[selector][key] != null) {
  259               preferencesBySelector[selector][key] = _.extend(value, preferencesBySelector[selector][key]);
  260             } else {
  261               preferencesBySelector[selector][key] = value;
  262             }
  263           }
  264         }
  265       }
  266       this.writeFileSync(path.join(destination, packageName + ".cson"), preferencesBySelector);
  267       return this.normalizeFilenames(destination);
  268     };
  269 
  270     PackageConverter.prototype.convertGrammars = function(source) {
  271       var child, childPath, destination, i, len, ref, sourceSyntaxes;
  272       sourceSyntaxes = path.join(source, 'syntaxes');
  273       if (!fs.isDirectorySync(sourceSyntaxes)) {
  274         sourceSyntaxes = path.join(source, 'Syntaxes');
  275       }
  276       if (!fs.isDirectorySync(sourceSyntaxes)) {
  277         return;
  278       }
  279       destination = path.join(this.destinationPath, 'grammars');
  280       ref = fs.readdirSync(sourceSyntaxes);
  281       for (i = 0, len = ref.length; i < len; i++) {
  282         child = ref[i];
  283         childPath = path.join(sourceSyntaxes, child);
  284         if (fs.isFileSync(childPath)) {
  285           this.convertFile(childPath, destination);
  286         }
  287       }
  288       return this.normalizeFilenames(destination);
  289     };
  290 
  291     return PackageConverter;
  292 
  293   })();
  294 
  295 }).call(this);