Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. const fs = require('fs-extra');
  18. const path = require('path');
  19. const tmp = require('tmp');
  20. const npa = require('npm-package-arg');
  21. const globby = require('globby');
  22. const isObject = require('isobject');
  23. const pathIsInside = require('path-is-inside');
  24. const requireFresh = require('import-fresh');
  25. const validateIdentifier = require('valid-identifier');
  26. const fetch = require('cordova-fetch');
  27. const { CordovaError, ConfigParser } = require('cordova-common');
  28. module.exports = cordovaCreate;
  29. /**
  30. * Creates a new cordova project in the given directory.
  31. *
  32. * @param {string} dest directory where the project will be created.
  33. * @param {Object} [opts={}] options to be used for creating the project.
  34. * @returns {Promise} Resolves when project creation has finished.
  35. */
  36. function cordovaCreate (dest, opts = {}) {
  37. let emit;
  38. // TODO this is to avoid having a huge diff. Remove later.
  39. let dir = dest;
  40. return Promise.resolve().then(() => {
  41. if (!dir) {
  42. throw new CordovaError('Directory not specified. See `cordova help`.');
  43. }
  44. if (!isObject(opts)) {
  45. throw new CordovaError('Given options must be an object');
  46. }
  47. // Shallow copy opts
  48. opts = Object.assign({}, opts);
  49. emit = getEventEmitter(opts);
  50. emit('verbose', 'Using detached cordova-create');
  51. // Make absolute.
  52. dir = path.resolve(dir);
  53. if (fs.existsSync(dir) && fs.readdirSync(dir).length > 0) {
  54. throw new CordovaError('Path already exists and is not empty: ' + dir);
  55. }
  56. if (opts.id && !validateIdentifier(opts.id)) {
  57. throw new CordovaError('App id contains a reserved word, or is not a valid identifier.');
  58. }
  59. if (!opts.template) {
  60. opts.template = require.resolve('cordova-app-hello-world');
  61. }
  62. // Ensure that the destination is outside the template location
  63. if (pathIsInside(dir, opts.template)) {
  64. throw new CordovaError(
  65. `Cannot create project "${dir}" inside the template used to create it "${opts.template}".`
  66. );
  67. }
  68. })
  69. .then(() => {
  70. // Finally, Ready to start!
  71. emit('log', 'Creating a new cordova project.');
  72. // Use cordova-fetch to obtain npm or git templates
  73. if (needsToBeFetched(opts.template)) {
  74. const target = opts.template;
  75. emit('verbose', 'Using cordova-fetch for ' + target);
  76. return fetch(target, getSelfDestructingTempDir(), {});
  77. } else {
  78. // If assets are not online, resolve as a relative path on local computer
  79. return path.resolve(opts.template);
  80. }
  81. })
  82. .then(templatePath => {
  83. let import_from_path;
  84. try {
  85. import_from_path = requireFresh(templatePath).dirname;
  86. } catch (e) {
  87. throw new CordovaError(templatePath + ' is not a valid template');
  88. }
  89. if (!fs.existsSync(import_from_path)) {
  90. throw new CordovaError('Could not find directory: ' +
  91. import_from_path);
  92. }
  93. const dirAlreadyExisted = fs.existsSync(dir);
  94. if (!dirAlreadyExisted) {
  95. fs.mkdirSync(dir);
  96. }
  97. try {
  98. // Copy files from template to project
  99. emit('verbose', 'Copying assets.');
  100. fs.copySync(import_from_path, dir);
  101. } catch (e) {
  102. if (!dirAlreadyExisted) {
  103. fs.removeSync(dir);
  104. }
  105. throw e;
  106. }
  107. // It is impossible to deploy .gitignore files via npm packages.
  108. // Instead, Cordova templates should include gitignore files that we
  109. // rename to .gitignore here. For more details see
  110. // https://github.com/apache/cordova-discuss/issues/69
  111. globby.sync(['**/gitignore'], { cwd: dir, absolute: true })
  112. .forEach(f =>
  113. fs.moveSync(f, path.join(path.dirname(f), '.gitignore'))
  114. );
  115. // Write out id, name and version to config.xml
  116. const configPath = path.join(dir, 'config.xml');
  117. const conf = new ConfigParser(configPath);
  118. conf.setPackageName(opts.id || conf.packageName() || 'com.example.cordova.app');
  119. conf.setName(opts.name || conf.name() || 'Cordova Example App');
  120. conf.setVersion(opts.version || conf.version() || '1.0.0');
  121. conf.write();
  122. // Copy values from config.xml to package.json
  123. const pkgJsonPath = path.join(dir, 'package.json');
  124. if (fs.existsSync(pkgJsonPath)) {
  125. const pkgJson = requireFresh(pkgJsonPath);
  126. Object.assign(pkgJson, {
  127. name: conf.packageName().toLowerCase(),
  128. displayName: conf.name(),
  129. version: conf.version()
  130. });
  131. fs.writeJsonSync(pkgJsonPath, pkgJson, { spaces: 2 });
  132. }
  133. });
  134. }
  135. function getEventEmitter ({ events }) {
  136. return events
  137. ? (...args) => events.emit(...args)
  138. : () => {};
  139. }
  140. // Creates temp dir that is deleted on process exit
  141. function getSelfDestructingTempDir () {
  142. return tmp.dirSync({
  143. prefix: 'cordova-create-',
  144. unsafeCleanup: true
  145. }).name;
  146. }
  147. function needsToBeFetched (uri) {
  148. return npa(uri).type !== 'directory';
  149. }