templateName.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.processTemplateName = processTemplateName;
  6. function _path() {
  7. const data = _interopRequireDefault(require("path"));
  8. _path = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _url() {
  14. const data = require("url");
  15. _url = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _fs() {
  21. const data = _interopRequireDefault(require("fs"));
  22. _fs = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _cliTools() {
  28. const data = require("@react-native-community/cli-tools");
  29. _cliTools = function () {
  30. return data;
  31. };
  32. return data;
  33. }
  34. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  35. const FILE_PROTOCOL = /file:/;
  36. const TARBALL = /\.tgz$/;
  37. const VERSION_POSTFIX = /(.*)(-\d+\.\d+\.\d+)/;
  38. const VERSIONED_PACKAGE = /(@?.+)(@)(.+)/;
  39. function handleFileProtocol(filePath) {
  40. let uri = new (_url().URL)(filePath).pathname;
  41. if (process.platform === 'win32') {
  42. // On Windows, the pathname has an extra / at the start, so remove that
  43. uri = uri.substring(1);
  44. }
  45. if (!_fs().default.existsSync(uri)) {
  46. throw new (_cliTools().CLIError)(`Failed to retrieve template name. The specified template directory path "${uri}" does not exist or is invalid.`);
  47. }
  48. const packageJsonPath = _path().default.join(uri, 'package.json');
  49. let packageJson;
  50. try {
  51. packageJson = JSON.parse(_fs().default.readFileSync(packageJsonPath, {
  52. encoding: 'utf8'
  53. }));
  54. } catch (_unused) {
  55. throw new (_cliTools().CLIError)('Failed to retrieve template name. We expect the template directory to include "package.json" file, but it was not found.');
  56. }
  57. if (!packageJson || !packageJson.name) {
  58. throw new (_cliTools().CLIError)(`Failed to retrieve template name. We expect the "package.json" of the template to include the "name" property, but we found "${packageJson ? packageJson.name : 'undefined'}" which is invalid.`);
  59. }
  60. return {
  61. uri,
  62. name: packageJson.name
  63. };
  64. }
  65. function handleTarball(filePath) {
  66. if (!_fs().default.existsSync(filePath)) {
  67. throw new (_cliTools().CLIError)(`Failed to retrieve tarball name. The specified tarball path "${filePath}" does not exist or is invalid.`);
  68. }
  69. const nameWithVersion = _path().default.parse(_path().default.basename(filePath)).name;
  70. const tarballVersionMatch = nameWithVersion.match(VERSION_POSTFIX);
  71. if (!tarballVersionMatch) {
  72. throw new (_cliTools().CLIError)(`Failed to retrieve tarball name. We expect the tarball to include package name and version, e.g.: "template-name-1.2.3-rc.0.tgz", but received: "${nameWithVersion}".`);
  73. }
  74. return {
  75. uri: filePath,
  76. name: tarballVersionMatch[1]
  77. };
  78. }
  79. function handleVersionedPackage(versionedPackage) {
  80. const versionedPackageMatch = versionedPackage.match(VERSIONED_PACKAGE);
  81. if (!versionedPackageMatch) {
  82. throw new (_cliTools().CLIError)(`Failed to retrieve package name. We expect the package to include name and version, e.g.: "template-name@1.2.3-rc.0", but received: "${versionedPackage}".`);
  83. }
  84. return {
  85. uri: versionedPackage,
  86. name: versionedPackageMatch[1]
  87. };
  88. }
  89. function processTemplateName(templateName) {
  90. if (templateName.match(TARBALL)) {
  91. return handleTarball(templateName);
  92. }
  93. if (templateName.match(FILE_PROTOCOL)) {
  94. return handleFileProtocol(templateName);
  95. }
  96. if (templateName.match(VERSIONED_PACKAGE)) {
  97. return handleVersionedPackage(templateName);
  98. }
  99. return {
  100. uri: templateName,
  101. name: templateName
  102. };
  103. }
  104. //# sourceMappingURL=templateName.js.map