|
| 1 | +import { copy, mkdirp, pathExists, readdirSafe, remove, stat } from '@ionic/utils-fs'; |
| 2 | +import chalk from 'chalk'; |
1 | 3 | import * as Debug from 'debug'; |
| 4 | +import * as lodash from 'lodash'; |
| 5 | +import * as os from 'os'; |
| 6 | +import * as path from 'path'; |
2 | 7 |
|
3 | | -import { BaseIntegration } from '../'; |
4 | | -import { InfoItem, IntegrationName, ProjectPersonalizationDetails } from '../../../definitions'; |
| 8 | +import { BaseIntegration, IntegrationConfig } from '../'; |
| 9 | +import { |
| 10 | + InfoItem, |
| 11 | + IntegrationAddDetails, IntegrationAddHandlers, |
| 12 | + IntegrationName, |
| 13 | + ProjectIntegration, |
| 14 | + ProjectPersonalizationDetails |
| 15 | +} from '../../../definitions'; |
5 | 16 |
|
6 | 17 | const debug = Debug('ionic:lib:integrations:cordova'); |
7 | 18 |
|
8 | | -export class Integration extends BaseIntegration { |
| 19 | +export class Integration extends BaseIntegration<ProjectIntegration> { |
9 | 20 | readonly name: IntegrationName = 'cordova'; |
10 | 21 | readonly summary = 'Target native iOS and Android with Apache Cordova'; |
11 | 22 | readonly archiveUrl = 'https://d2ql0qc7j8u4b2.cloudfront.net/integration-cordova.tar.gz'; |
12 | 23 |
|
| 24 | + get config(): IntegrationConfig { |
| 25 | + return new IntegrationConfig(this.e.project.filePath, { pathPrefix: ['integrations', this.name] }); |
| 26 | + } |
| 27 | + |
| 28 | + async add(details: IntegrationAddDetails): Promise<void> { |
| 29 | + const handlers: IntegrationAddHandlers = { |
| 30 | + conflictHandler: async (f, stats) => { |
| 31 | + const isDirectory = stats.isDirectory(); |
| 32 | + const filename = `${path.basename(f)}${isDirectory ? '/' : ''}`; |
| 33 | + const type = isDirectory ? 'directory' : 'file'; |
| 34 | + |
| 35 | + const confirm = await details.env.prompt({ |
| 36 | + type: 'confirm', |
| 37 | + name: 'confirm', |
| 38 | + message: `The ${chalk.cyan(filename)} ${type} exists in project. Overwrite?`, |
| 39 | + default: false, |
| 40 | + }); |
| 41 | + |
| 42 | + return confirm; |
| 43 | + }, |
| 44 | + onFileCreate: f => { |
| 45 | + if (!details.quiet) { |
| 46 | + details.env.log.msg(`${chalk.green('CREATE')} ${f}`); |
| 47 | + } |
| 48 | + }, |
| 49 | + }; |
| 50 | + const onFileCreate = handlers.onFileCreate ? handlers.onFileCreate : lodash.noop; |
| 51 | + const conflictHandler = handlers.conflictHandler ? handlers.conflictHandler : async () => false; |
| 52 | + |
| 53 | + const { createRequest, download } = await import('../../utils/http'); |
| 54 | + const { tar } = await import('../../utils/archive'); |
| 55 | + |
| 56 | + this.e.log.info(`Downloading integration ${chalk.green(this.name)}`); |
| 57 | + const tmpdir = path.resolve(os.tmpdir(), `ionic-integration-${this.name}`); |
| 58 | + |
| 59 | + // TODO: etag |
| 60 | + |
| 61 | + if (await pathExists(tmpdir)) { |
| 62 | + await remove(tmpdir); |
| 63 | + } |
| 64 | + |
| 65 | + await mkdirp(tmpdir); |
| 66 | + |
| 67 | + const ws = tar.extract({ cwd: tmpdir }); |
| 68 | + const { req } = await createRequest('GET', this.archiveUrl, this.e.config.getHTTPConfig()); |
| 69 | + await download(req, ws, {}); |
| 70 | + |
| 71 | + const contents = await readdirSafe(tmpdir); |
| 72 | + const blacklist: string[] = []; |
| 73 | + |
| 74 | + debug(`Integration files downloaded to ${chalk.bold(tmpdir)} (files: ${contents.map(f => chalk.bold(f)).join(', ')})`); |
| 75 | + |
| 76 | + for (const f of contents) { |
| 77 | + const projectf = path.resolve(this.e.project.directory, f); |
| 78 | + |
| 79 | + try { |
| 80 | + const stats = await stat(projectf); |
| 81 | + const overwrite = await conflictHandler(projectf, stats); |
| 82 | + |
| 83 | + if (!overwrite) { |
| 84 | + blacklist.push(f); |
| 85 | + } |
| 86 | + } catch (e) { |
| 87 | + if (e.code !== 'ENOENT') { |
| 88 | + throw e; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + this.e.log.info(`Copying integrations files to project`); |
| 94 | + debug(`Blacklist: ${blacklist.map(f => chalk.bold(f)).join(', ')}`); |
| 95 | + |
| 96 | + await mkdirp(details.root); |
| 97 | + |
| 98 | + await copy(tmpdir, details.root, { |
| 99 | + filter: f => { |
| 100 | + if (f === tmpdir) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + const projectf = f.substring(tmpdir.length + 1); |
| 105 | + |
| 106 | + for (const item of blacklist) { |
| 107 | + if (item.slice(-1) === '/' && `${projectf}/` === item) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + if (projectf.startsWith(item)) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + onFileCreate(projectf); |
| 117 | + |
| 118 | + return true; |
| 119 | + }, |
| 120 | + }); |
| 121 | + await super.add(details); |
| 122 | + } |
| 123 | + |
13 | 124 | async getInfo(): Promise<InfoItem[]> { |
14 | 125 | const { getAndroidSdkToolsVersion } = await import('./android'); |
15 | 126 |
|
|
0 commit comments