Skip to content

Commit

Permalink
Capacitor (#198)
Browse files Browse the repository at this point in the history
* chore: update error message if non-Monaca project

* chore: change message for monaca init

* feat: skip checking config.xml when creating capacitor project

* MN-4689: Support Commands for Capacitor Projects (#200)

* MN-4689: display message for not-supported commands

* MN-4689: show/hide command info on project-level

* filter command by task name instead of task set

* support yarn (#201)

* update Changelog

* pinn monaca-lib
  • Loading branch information
yong-asial authored Jan 11, 2024
1 parent 3ca1676 commit 8068e04
Show file tree
Hide file tree
Showing 16 changed files with 310 additions and 52 deletions.
222 changes: 188 additions & 34 deletions CHANGELOG.md

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions doc/tasks/remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": false,
"react-native": true
},
"description": "build a project on Monaca Cloud",
Expand Down Expand Up @@ -82,6 +83,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": false,
"react-native": true
},
"description": "open the project configuration on Monaca Cloud",
Expand Down
1 change: 1 addition & 0 deletions doc/tasks/serve.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": true,
"react-native": false
},
"description": "run a local web server for preview (execute `npm run monaca:preview` defined in package.json)",
Expand Down
1 change: 1 addition & 0 deletions doc/tasks/signing.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": false,
"react-native": true
},
"description": "manage signing configurations",
Expand Down
3 changes: 3 additions & 0 deletions doc/tasks/sync.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": true,
"react-native": false
},
"description": "upload a project to Monaca Cloud",
Expand Down Expand Up @@ -84,6 +85,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": true,
"react-native": false
},
"description": "download a project from Monaca Cloud",
Expand Down Expand Up @@ -121,6 +123,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": true,
"react-native": false
},
"description": "run app on device using Monaca Debugger (execute `npm run monaca:debug` defined in package.json)",
Expand Down
1 change: 1 addition & 0 deletions doc/tasks/transpile.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": true,
"react-native": false
},
"description": "transpile project source code (execute `npm run monaca:transpile` defined in package.json)",
Expand Down
1 change: 1 addition & 0 deletions doc/tasks/upgrade.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"category": "project",
"frameworkSupport": {
"cordova": true,
"capacitor": false,
"react-native": false
},
"description": "update old projects to Monaca CLI 3.x structure",
Expand Down
14 changes: 8 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "monaca",
"version": "4.2.11",
"version": "5.0.0",
"description": "Monaca Command Line Tool",
"bin": {
"monaca": "bin/monaca"
Expand Down Expand Up @@ -36,7 +36,7 @@
"ip": "^1.1.5",
"minimist": "^1.2.8",
"monaca-inquirer": "^1.0.4",
"monaca-lib": "^4.1.10",
"monaca-lib": "^5.0.0",
"opn": "^5.3.0",
"portfinder": "^1.0.7",
"q": "^1.1.2",
Expand Down
36 changes: 36 additions & 0 deletions src/capacitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(function() {
'use strict';
let Capacitor = {};

/**
* Not-supported command names.
*/
Capacitor.notSupportedTask = [
'remote config',
'remote build',
'signing',
'init',
'upgrade',
'update',
'debug',
'plugin',
'platform',
'prepare',
'compile',
'run',
'build',
'emulate',
];

/**
* Is not supported for Capacitor.
*
* @param {string} taskName
* @return {boolean}
*/
Capacitor.isNotSupportedTask = function (taskName) {
return Capacitor.notSupportedTask.includes(taskName);
};

module.exports = Capacitor;
}());
10 changes: 8 additions & 2 deletions src/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var fs = require('fs'),
serializer = new XMLSerializer(),
Monaca = require('monaca-lib').Monaca,
util = require(path.join(__dirname, 'util')),
lib = require(path.join(__dirname, 'lib')),
sync = require(path.join(__dirname, 'sync'));

var isWindowsPlatform = process.platform === 'win32';
Expand Down Expand Up @@ -62,12 +63,17 @@ CreateTask.run = function(taskName, info) {
};

CreateTask.createApp = function(template) {
var error = 'Error occurred while creating project: ';
let error = 'Error occurred while creating project: ';
report.arg1 = template.name;

monaca.downloadTemplate(template.resource, path.resolve(dirName))
.then(
function() {
// if it is capacitor project, skip checking config.xml
if (lib.isCapacitorProject(dirName)) {
return Promise.resolve();
}

// Extract the project name if nested path is given before project name.
// E.g. if command is 'create project Apps/Finance/myFinanceApp', then myFinanceApp will be taken as project name.
error = 'An error occurred while injecting project name in config.xml: ';
Expand All @@ -81,7 +87,7 @@ CreateTask.createApp = function(template) {
.then(
function() {
util.success('\nProject is created successfully.')
var message = [
let message = [
'',
'Type "cd ' + dirName + '" and run monaca command again.',
' > ' + 'monaca preview'.info + ' => Run app in the browser',
Expand Down
4 changes: 2 additions & 2 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let printInitInfo = (commands) => {
// www folder
util.print(`A. Change the output folder to ${'www'.commands}.`);
// config.xml
util.print(`B. In case of not having a ${'config.xml'.commands} file, a new one has been created with Monaca's default settings.`);
util.print(`B. For Cordova project, a new ${'config.xml'.commands} file has been created with Monaca's default settings.`);
// monaca commands
util.print(`C. Some new commands have been added:\n`
+ `\t${'monaca:preview'.commands}: ${commands.serve.commands}\n`
Expand All @@ -31,7 +31,7 @@ let printInitInfo = (commands) => {
+ `\t<!--Load selected CSS libraries-->\n`
+ `\t<link rel="stylesheet" href="components/loader.css”>`);
// plugins
util.print(`F. Cordova plugins are managed by ${'package.json'.commands} file. If your plugin information is defined in ${'config.xml'.commands} file, you will need to import them again into Monaca.`);
util.print(`F. Cordova/Capacitor plugins are managed by ${'package.json'.commands} file. If your plugin information is defined in ${'config.xml'.commands} file, you will need to import them again into Monaca.`);
// monaca documentation url
util.print(`G. For more details about the changes, please refer to ${CLI_MIGRATION_DOC_URL.url}`);
}
Expand Down
30 changes: 26 additions & 4 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,47 @@ var VERSION = require(path.join(__dirname, '..', 'package.json')).version;

var findProjectDir = function(cwd, monaca) {
return monaca.isMonacaProject(cwd).then(
(type) => {
() => {
return Q.resolve(cwd);
},
(error) => {
const errMessage = `Directory is not a Monaca project: 'config.xml' file, 'www' folder or '.monaca' folder may be missing.\nPlease visit https://en.docs.monaca.io/products_guide/monaca_cli/troubleshooting#incomplete-files-and-folder-structure.\n\nPlease execute 'monaca init' to initialize your project in case of having been created using another CLI tool.`;
() => {

const errMessage = `\n\nThis does not appear to be a Monaca project.
\nPlease visit https://en.docs.monaca.io/products_guide/monaca_cli/troubleshooting#incomplete-files-and-folder-structure for more information on incomplete files and folder structure.
\nTo initialize your project with Monaca, please execute 'monaca init'. Monaca currently supports Cordova and Capacitor projects`;
let newPath = path.join(cwd, '..');
return newPath === cwd ? Q.reject(errMessage) : findProjectDir(newPath, monaca);
}
);
};

const isCapacitorProject = (cwd = null) => {
if (!cwd) {
cwd = process.cwd();
}
let projectConfig;

try {
projectConfig = require(path.resolve(cwd, 'package.json'));
if (projectConfig?.dependencies && projectConfig.dependencies['@capacitor/core']) {
return true;
}
} catch (err) {}

return false;
};

var softlyAssureMonacaProject = function(cwd) {
var projectConfig;

try {
projectConfig = require(path.resolve(cwd, 'package.json'));
} catch (err) {}

if (projectConfig && projectConfig.dependencies && projectConfig.dependencies['react-native']) {
if (projectConfig?.dependencies && projectConfig.dependencies['react-native']) {
return 'react-native';
} else if (isCapacitorProject(cwd)) {
return 'capacitor';
} else if (fs.existsSync(path.resolve(cwd, 'www')) && fs.existsSync(path.join(cwd, 'config.xml'))) {
return 'cordova';
} else {
Expand Down Expand Up @@ -467,6 +488,7 @@ const DEBUGGER_TROUBLESHOOTING_DOC_URL = 'https://en.docs.monaca.io/products_gui
const DEBUGGER_USAGE_DOC_URL = 'https://en.docs.monaca.io/products_guide/debugger/debug/#monaca-debugger-with-monaca-local-development-tools';

module.exports = {
isCapacitorProject: isCapacitorProject,
findProjectDir: findProjectDir,
assureMonacaProject: assureMonacaProject,
confirmOverwrite: confirmOverwrite,
Expand Down
6 changes: 6 additions & 0 deletions src/monaca.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var argv = require('minimist')(process.argv.slice(2)),
path = require('path'),
https = require('https'),
lib = require(path.join(__dirname, 'lib')),
capacitor = require(path.join(__dirname, 'capacitor')),
util = require(path.join(__dirname, 'util')),
terminal = require(path.join(__dirname, 'terminal'));

Expand Down Expand Up @@ -110,6 +111,11 @@ var Monaca = {
process.exit(0);
}

// Check command not-supported for Capacitor project
if (lib.isCapacitorProject() && capacitor.isNotSupportedTask(task.name)) {
util.fail('This command is not supported yet for Capacitor project. Please use Monaca Cloud IDE to perform the task.');
}

if (argv.help || argv.h
|| (task.name === 'create' && argv._.length < 2 && !argv['template-list'])
|| (task.name === 'docs' && argv._.length < 2)
Expand Down
8 changes: 6 additions & 2 deletions src/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ ServeTask.run = function (taskName, info) {
.then( dir => {
lib.needToUpgrade(dir, monaca);
try {
let command = 'npm';
if (util.isUsingYarn(dir)) {
command = 'yarn';
}
if (process.platform !== 'win32') {
spawn('npm', ['run', 'monaca:preview'], {stdio: 'inherit'});
spawn(command, ['run', 'monaca:preview'], {stdio: 'inherit'});
} else {
console.log('Running "monaca preview" on a separate terminal console. To exit this main process, please close the opened terminal windows.');
const childProcess = spawn('npm.cmd', ['run', 'monaca:preview'], {stdio: 'ignore', detached: true});
const childProcess = spawn(command + '.cmd', ['run', 'monaca:preview'], {stdio: 'ignore', detached: true});
childProcess.on('close', (data) => {
console.log('\n\nExiting Program...');
process.exit(data);
Expand Down
19 changes: 19 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

var Q = require('q'),
fs = require('fs'),
path = require('path'),
colors =require('colors');

var UPDATE_INTERVAL = 21600; //6 hours
Expand Down Expand Up @@ -248,6 +249,23 @@ var getFormatExpirationDate = (timestamp) => {
}
}

/**
* Checks if a project is using Yarn as its package manager.
*
* @param {string} projectDir - The directory path of the project.
* @returns {boolean} - Returns true if the project uses Yarn, false otherwise.
*/
const isUsingYarn = (projectDir) => {
try {
const projectConfig = require(path.join(projectDir, 'package.json'));
const monacaPreviewScript = projectConfig.scripts && projectConfig.scripts['monaca:preview'];
if (monacaPreviewScript && monacaPreviewScript.indexOf('yarn') >= 0) {
return true;
}
} catch (err) {}
return false;
};

module.exports = {
print: println,
info: printinfo,
Expand All @@ -268,5 +286,6 @@ module.exports = {
validateCountryCode: validateCountryCode,
validateRequireField: validateRequireField,
getFormatExpirationDate: getFormatExpirationDate,
isUsingYarn: isUsingYarn,
};
})();

0 comments on commit 8068e04

Please sign in to comment.