2016-01-12 00:03:36 +03:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for details.
|
2016-01-09 00:37:34 +03:00
|
|
|
|
2018-05-30 15:13:39 +03:00
|
|
|
import * as child_process from "child_process";
|
|
|
|
import * as fs from "fs";
|
|
|
|
import * as http from "http";
|
|
|
|
import * as os from "os";
|
2016-01-09 00:37:34 +03:00
|
|
|
|
2022-06-22 15:32:48 +03:00
|
|
|
import { CordovaProjectHelper } from "../src/utils/cordovaProjectHelper";
|
2016-01-09 00:37:34 +03:00
|
|
|
|
2021-08-19 17:02:13 +03:00
|
|
|
export function executeCordovaCommand(cwd: string, command: string): Promise<any> {
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-07-18 11:21:26 +03:00
|
|
|
const cordovaCmd = os.platform() === "win32" ? "cordova.cmd" : "cordova";
|
|
|
|
const commandToExecute = `${cordovaCmd} ${command}`;
|
|
|
|
const process = child_process.exec(commandToExecute, { cwd });
|
2021-08-19 17:02:13 +03:00
|
|
|
|
|
|
|
let stderr = "";
|
|
|
|
process.stderr.on("data", (data: Buffer) => {
|
|
|
|
stderr += data.toString();
|
|
|
|
});
|
|
|
|
|
|
|
|
process.stdout.on("data", (data: Buffer) => {
|
|
|
|
console.log(data.toString());
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on("error", function (err: any): void {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
|
2022-06-22 15:32:48 +03:00
|
|
|
process.on("close", exitCode => {
|
2021-08-19 17:02:13 +03:00
|
|
|
if (exitCode !== 0) {
|
2022-06-22 15:32:48 +03:00
|
|
|
return reject(
|
|
|
|
`Cordova command failed with exit code ${exitCode}. Error: ${stderr}`,
|
|
|
|
);
|
2021-08-19 17:02:13 +03:00
|
|
|
}
|
|
|
|
resolve({});
|
|
|
|
});
|
2016-01-09 00:37:34 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-19 17:02:13 +03:00
|
|
|
export function createCordovaProject(cwd: string, projectName: string): Promise<any> {
|
2022-07-18 11:21:26 +03:00
|
|
|
const cordovaCommandToRun = `create ${projectName}`;
|
2016-01-09 00:37:34 +03:00
|
|
|
return executeCordovaCommand(cwd, cordovaCommandToRun);
|
|
|
|
}
|
|
|
|
|
2022-06-22 15:32:48 +03:00
|
|
|
export function addCordovaComponents(
|
|
|
|
componentName: string,
|
|
|
|
projectRoot: string,
|
|
|
|
componentsToAdd: string[],
|
|
|
|
): Promise<any> {
|
2022-07-18 11:21:26 +03:00
|
|
|
const cordovaCommandToRun = `${componentName} add ${componentsToAdd.join(" ")}`;
|
2016-01-09 00:37:34 +03:00
|
|
|
return executeCordovaCommand(projectRoot, cordovaCommandToRun);
|
|
|
|
}
|
|
|
|
|
2022-06-22 15:32:48 +03:00
|
|
|
export function removeCordovaComponents(
|
|
|
|
componentName: string,
|
|
|
|
projectRoot: string,
|
|
|
|
componentsToRemove: string[],
|
|
|
|
): Promise<any> {
|
2022-07-18 11:21:26 +03:00
|
|
|
const cordovaCommandToRun = `${componentName} remove ${componentsToRemove.join(" ")}`;
|
2016-01-09 00:37:34 +03:00
|
|
|
return executeCordovaCommand(projectRoot, cordovaCommandToRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function enumerateListOfTypeDefinitions(projectRoot: string): string[] {
|
2022-07-18 11:21:26 +03:00
|
|
|
const typeDefsFolder = CordovaProjectHelper.getCordovaPluginTypeDefsPath(projectRoot);
|
2016-01-09 01:32:39 +03:00
|
|
|
|
2016-01-09 00:37:34 +03:00
|
|
|
// look for all the type defs in the typings folder
|
2016-01-12 00:03:36 +03:00
|
|
|
if (!CordovaProjectHelper.existsSync(typeDefsFolder)) {
|
2016-01-09 00:37:34 +03:00
|
|
|
return [];
|
|
|
|
}
|
2022-07-18 11:21:26 +03:00
|
|
|
return fs.readdirSync(typeDefsFolder);
|
2016-01-09 00:37:34 +03:00
|
|
|
}
|
2016-04-19 02:09:13 +03:00
|
|
|
|
2021-08-19 17:02:13 +03:00
|
|
|
export function isUrlReachable(url: string): Promise<boolean> {
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-06-22 15:32:48 +03:00
|
|
|
http.get(url, res => {
|
2021-08-19 17:02:13 +03:00
|
|
|
resolve(true);
|
|
|
|
res.resume();
|
|
|
|
}).on("error", (_err: Error) => {
|
|
|
|
resolve(false);
|
|
|
|
});
|
2016-04-19 02:09:13 +03:00
|
|
|
});
|
|
|
|
}
|
2020-10-09 17:36:39 +03:00
|
|
|
|
2022-07-18 11:21:26 +03:00
|
|
|
export function convertWindowsPathToUnixOne(path: string): string {
|
2020-10-09 17:36:39 +03:00
|
|
|
return path.replace(/\\/g, "/");
|
|
|
|
}
|