ckeditor5-dirs.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const path = require( 'path' );
  7. const tools = require( './tools' );
  8. const git = require( './git' );
  9. const dependencyRegExp = /^ckeditor5-/;
  10. module.exports = {
  11. /**
  12. * Returns dependencies that starts with `ckeditor5-`, and have valid, short GitHub url. Returns `null` if no
  13. * dependencies are found.
  14. *
  15. * @param {Object} dependencies Dependencies object loaded from `package.json` file.
  16. * @returns {Object|null}
  17. */
  18. getDependencies( dependencies ) {
  19. let result = null;
  20. if ( dependencies ) {
  21. Object.keys( dependencies ).forEach( ( key ) => {
  22. if ( dependencyRegExp.test( key ) ) {
  23. if ( result === null ) {
  24. result = {};
  25. }
  26. result[ key ] = dependencies[ key ];
  27. }
  28. } );
  29. }
  30. return result;
  31. },
  32. /**
  33. * Returns all directories under specified path that match `ckeditor5-*` pattern.
  34. *
  35. * @param {String} path
  36. * @returns {Array.<String>}
  37. */
  38. getDirectories( path ) {
  39. return tools.getDirectories( path ).filter( dir => {
  40. return dependencyRegExp.test( dir );
  41. } );
  42. },
  43. /**
  44. * Returns a list of symbolic links to directories with names starting with `ckeditor5-` prefix.
  45. *
  46. * @param {String} path Path to directory,
  47. * @returns {Array} Array with directories names.
  48. */
  49. getSymlinks( path ) {
  50. const fs = require( 'fs' );
  51. const pth = require( 'path' );
  52. return fs.readdirSync( path ).filter( item => {
  53. const fullPath = pth.join( path, item );
  54. return dependencyRegExp.test( item ) && tools.isSymlink( fullPath );
  55. } );
  56. },
  57. /**
  58. * Returns an array with information about `ckeditor5-*` directories in development mode.
  59. *
  60. * @param {String} workspacePath Absolute path to workspace.
  61. * @param {Object} packageJSON Contents of `ckeditor5` repo `package.json` file.
  62. * @param {String} ckeditor5Path Absolute path to ckeditor5 root directory.
  63. * @param {Boolean} includeRoot Include main `ckeditor5` package.
  64. * @returns {Array.<Object>}
  65. */
  66. getDevDirectories( workspacePath, packageJSON, ckeditor5Path, includeRoot ) {
  67. const directories = this.getDirectories( workspacePath );
  68. const dependencies = this.getDependencies( packageJSON.dependencies );
  69. let devDirectories = [];
  70. for ( let dependency in dependencies ) {
  71. const repositoryURL = dependencies[ dependency ];
  72. const urlInfo = git.parseRepositoryUrl( repositoryURL );
  73. const repositoryPath = path.join( ckeditor5Path, 'node_modules', dependency );
  74. // Check if repository's directory already exists.
  75. if ( directories.indexOf( urlInfo.name ) > -1 ) {
  76. devDirectories.push( {
  77. repositoryPath,
  78. repositoryURL
  79. } );
  80. }
  81. }
  82. if ( includeRoot ) {
  83. // Add root dependency and directory.
  84. devDirectories.unshift( {
  85. repositoryPath: ckeditor5Path,
  86. repositoryURL: 'ckeditor/ckeditor5'
  87. } );
  88. }
  89. return devDirectories;
  90. }
  91. };