How to increment the version number of an Ionic 3 project? - ionic-framework

Is there a quick and easy way to increment the version number of an Ionic 3 project without having to manually edit each file where the version number appears?
I am thinking of config.xml, package.json, src/index.html and every other place inside the project where the version number appears.
Say I want to increment the version number from 2.3.4 to 2.3.5 after I've made some updates to the code. I would love to have something like
$ ionic run version-increment [major][middle][minor]
which would edit all the files at once.
Is there a plugin or a magic trick that could do that for me?

Ideally you should tie version updates to your build process. This way when you initiate build process you auto-increment version of your app.
You could do it this way in Ionic 4:
Step 1: Update ionic.config.json to feature a pre-build hook:
{
"name": "morphistic-app",
"type": "angular",
"id": "b8d9b232",
"integrations": {
"cordova": {}
},
"hooks": {
"build:before": "./config/build-before.js"
}
}
Step 2: Create build-before.js and add the following:
const fs = require('fs');
module.exports = (ctx) => {
if (ctx.build && ctx.build.configuration && ctx.build.configuration === "production") {
console.log("production build: performing version bump...");
// update package.json:
let packageJSON = JSON.parse(fs.readFileSync('package.json', 'utf-8').toString());
let versionArray = packageJSON.version.split(".");
versionArray[2] = (parseInt(versionArray[2])+1).toString();
packageJSON.version = versionArray.join(".");
fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, "\t"), 'utf-8');
console.log("package.json app version updated");
let prodEnvData = fs.readFileSync(`src/environments/environment.prod.ts`, 'utf-8');
prodEnvData = prodEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
fs.writeFileSync('src/environments/environment.prod.ts', prodEnvData, 'utf-8');
console.log("environments.prod.ts app version updated");
let devEnvData = fs.readFileSync(`src/environments/environment.dev.ts`, 'utf-8');
devEnvData = devEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
fs.writeFileSync('src/environments/environment.dev.ts', devEnvData, 'utf-8');
console.log("environments.dev.ts app version updated");
let localEnvData = fs.readFileSync(`src/environments/environment.local.ts`, 'utf-8');
localEnvData = localEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
fs.writeFileSync('src/environments/environment.local.ts', localEnvData, 'utf-8');
console.log("environments.local.ts app version updated");
let defaultEnvData = fs.readFileSync(`src/environments/environment.ts`, 'utf-8');
defaultEnvData = defaultEnvData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${packageJSON.version}"`);
fs.writeFileSync('src/environments/environment.ts', defaultEnvData, 'utf-8');
console.log("environments.ts app version updated");
let configXmlData = fs.readFileSync('config.xml', 'utf-8');
configXmlData = configXmlData.replace(/id="com.yourapp.ionic" version=".*"/, `id="com.yourapp.ionic" version="${packageJSON.version}"`);
fs.writeFileSync('config.xml', configXmlData,'utf-8');
console.log("config.xml app version updated");
};
};
The code above also handles environment file changes, you can skip that if it is not your goal.
if you need to update any html file - see above example with config.xml
Also the code above assumes your version is a semver compatible schema aka "n.n.n"
Alternatively in ionic 3 you could do:
Step 1: update your 'scripts' property inside package.json to point at your built step script:
"scripts": {
"clean": "ionic-app-scripts clean",
"build": "ionic-app-scripts build",
"lint": "ionic-app-scripts lint",
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve --non-interactive",
"ionic:serve:before": "cross-env MY_ENV='dev' node ./config/env.config.js",
"ionic:build:before": "cross-env MY_ENV='prod' node ./config/env.config.js",
}
Please note I am using cross-env library to ensure environment variable can be available in the context of the script below:
Step 2: create env.config.js file:
var fs = require('fs');
function readWriteSync() {
let env = process.env.MY_ENV || 'dev';
console.log("updating configuration for: ", env);
// read env data:
let envData = fs.readFileSync(`config/environment.${env}.ts`, 'utf-8');
// read data from package.json:
let packageJSON = JSON.parse(fs.readFileSync('package.json', 'utf-8').toString());
let version;
if (env === "prod") {
let versionArray = packageJSON.version.split(".");
versionArray[2] = (parseInt(versionArray[2])+1).toString();
packageJSON.version = versionArray.join(".");
// write to package.json:
fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, "\t"), 'utf-8');
};
version = packageJSON.version;
// replace env data:
envData = envData.replace(/CURRENT_VERSION: ".*"/, `CURRENT_VERSION: "${version}"`);
// write env data
fs.writeFileSync('src/environments/environment.ts', envData, 'utf-8');
// update config.xml:
let configXmlData = fs.readFileSync('config.xml', 'utf-8');
configXmlData = configXmlData.replace(/id="com.yourapp.ionic" version=".*"/, `id="com.yourapp.ionic" version="${version}"`);
fs.writeFileSync('config.xml', configXmlData,'utf-8');
};
readWriteSync();

I have a big Ionic/ Cordova Project and the only Place where you really need to have the version number is the config.xml. You should make your App to work with the number only there and then the manually change is not a big deal.

Related

Getting this error with the #metaplex-foundation/js-next SDK

Currently working on a react app and I'm getting this error after installing metaplex.
My react-scripts version is 4.0.3
./node_modules/#metaplex-foundation/js-next/dist/esm/programs/token/gpaBuilders/TokenGpaBuilder.mjs
Can't import the named export 'ACCOUNT_SIZE' from non EcmaScript module (only default export is available)
I found out the solution in the git of metaplex here. I will leave you here the whole answer anyway.
Getting Started with Metaplex and CRA 5
This example sets up a new React app with Metaplex using "Create React App" (CRA) version 5 β€” i.e. using Webpack 5.
This example has been generated using the following steps:
Create a new project using the "Create React App" command.
npx create-react-app getting-started-react-cra5
cd getting-started-react-cra5
Install the Metaplex and the Solana SDKs.
npm install #metaplex-foundation/js #solana/web3.js
Install some polyfills.
npm install assert util crypto-browserify stream-browserify
Install and use react-app-rewired.
# Installs react-app-rewired.
npm install react-app-rewired
# Replaces "react-scripts" with "react-app-rewired" in package.json scripts.
sed -i '' 's/react-scripts /react-app-rewired /g' package.json
Override Webpack 5 configurations.
Create a new file to override Webpack 5 configurations.
touch config-overrides.js
Copy the following code inside the new config-overrides.js file.
const webpack = require("webpack");
module.exports = function override(webpackConfig) {
// Disable resolving ESM paths as fully specified.
// See: https://github.com/webpack/webpack/issues/11467#issuecomment-691873586
webpackConfig.module.rules.push({
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
});
// Ignore source map warnings from node_modules.
// See: https://github.com/facebook/create-react-app/pull/11752
webpackConfig.ignoreWarnings = [/Failed to parse source map/];
// Polyfill Buffer.
webpackConfig.plugins.push(
new webpack.ProvidePlugin({ Buffer: ["buffer", "Buffer"] })
);
// Polyfill other modules.
webpackConfig.resolve.fallback = {
crypto: require.resolve("crypto-browserify"),
stream: require.resolve("stream-browserify"),
util: require.resolve("util"),
assert: require.resolve("assert"),
fs: false,
process: false,
path: false,
zlib: false,
};
return webpackConfig;
};
Update your browser requirements.
Update the browserslist object of your package.json to include the following production requirements.
"browserslist": {
"production": [
- ">0.2%",
- "not dead",
- "not op_mini all"
+ "chrome >= 67",
+ "edge >= 79",
+ "firefox >= 68",
+ "opera >= 54",
+ "safari >= 14"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
That's it!
You should not use #metaplex-foundation/js-next SDK, the actual repo is #metaplex-foundation/js, the name was changed and the updated repo is this, so try using #metaplex-foundation/js instead

Azure Terraform function app deployment issue

I hope somebody can help me with this issue because I don't understand what am I doing wrong.
I am trying to build an azure function app and deploy a zip package (timer trigger) to it.
I have set this code.
resource "azurerm_resource_group" "function-rg" {
location = "westeurope"
name = "resource-group"
}
data "azurerm_storage_account_sas" "sas" {
connection_string = azurerm_storage_account.sthriprdeurcsvtoscim.primary_connection_string
https_only = true
start = "2021-01-01"
expiry = "2023-12-31"
resource_types {
object = true
container = false
service = false
}
services {
blob = true
queue = false
table = false
file = false
}
permissions {
read = true
write = false
delete = false
list = false
add = false
create = false
update = false
process = false
}
}
resource "azurerm_app_service_plan" "ASP-rg-hri-prd-scim" {
location = azurerm_resource_group.function-rg.location
name = "ASP-rghriprdeurcsvtoscim"
resource_group_name = azurerm_resource_group.function-rg.name
kind = "functionapp"
maximum_elastic_worker_count = 1
per_site_scaling = false
reserved = false
sku {
capacity = 0
size = "Y1"
tier = "Dynamic"
}
}
resource "azurerm_storage_container" "deployments" {
name = "function-releases"
storage_account_name = azurerm_storage_account.sthriprdeurcsvtoscim.name
container_access_type = "private"
}
resource "azurerm_storage_blob" "appcode" {
name = "functionapp.zip"
storage_account_name = azurerm_storage_account.sthriprdeurcsvtoscim.name
storage_container_name = azurerm_storage_container.deployments.name
type = "Block"
source = "./functionapp.zip"
}
resource "azurerm_function_app" "func-hri-prd-eur-csv-to-scim" {
storage_account_name = azurerm_storage_account.sthriprdeurcsvtoscim.name
storage_account_access_key = azurerm_storage_account.sthriprdeurcsvtoscim.primary_access_key
app_service_plan_id = azurerm_app_service_plan.ASP-rg-hri-prd-scim.id
location = azurerm_resource_group.function-rg.location
name = "func-hri-prd-csv-to-scim"
resource_group_name = azurerm_resource_group.function-rg.name
app_settings = {
"WEBSITE_RUN_FROM_PACKAGE" = "https://${azurerm_storage_account.sthriprdeurcsvtoscim.name}.blob.core.windows.net/${azurerm_storage_container.deployments.name}/${azurerm_storage_blob.appcode.name}${data.azurerm_storage_account_sas.sas.sas}"
"FUNCTIONS_EXTENSION_VERSION" = "~3"
"FUNCTIONS_WORKER_RUNTIME" = "dotnet"
}
enabled = true
identity {
type = "SystemAssigned"
}
version = "~3"
enable_builtin_logging = false
}
resource "azurerm_storage_account" "sthriprdeurcsvtoscim" {
account_kind = "Storage"
account_replication_type = "LRS"
account_tier = "Standard"
allow_blob_public_access = false
enable_https_traffic_only = true
is_hns_enabled = false
location = azurerm_resource_group.function-rg.location
name = "sthriprdeurcsvtoscim"
resource_group_name = azurerm_resource_group.function-rg.name
}
Goes without saying that terraform apply work without any error. The configurations of the function app are correct and points to the right storage account. The storage account has a container with the zip file containing my azure function code.
But when I go to the function app -> Functions, I don't see any function there.
Can please somebody help me to understand what am I doing wrong in this?
The Function app is a .net3 function
When you create a function app, it isn’t set up for Functions + Terraform. It’s set up for a Visual Code + Functions deployment. We need to adjust both the package.json so that it will produce the ZIP file for us, and the .gitignore so that it ignores the Terraform build files. I use a bunch of helper NPM packages:
azure-functions-core-tools for the func command.
#ffflorian/jszip-cli to ZIP my files up.
mkdirp for creating directories.
npm-run-all and particularly the run-s command for executing things in order.
rimraf for deleting things.
Below is the code how package.json looks like
{
"name": "backend",
"version": "1.0.0",
"description": "",
"scripts": {
"func": "func",
"clean": "rimraf build",
"build:compile": "tsc",
"build:prune": "npm prune --production",
"prebuild:zip": "mkdirp --mode=0700 build",
"build:zip": "jszip-cli",
"build": "run-s clean build:compile build:zip",
"predeploy": "npm run build",
"deploy": "terraform apply"
},
"dependencies": {
},
"devDependencies": {
"azure-functions-core-tools": "^2.7.1724",
"#azure/functions": "^1.0.3",
"#ffflorian/jszip-cli": "^3.0.2",
"mkdirp": "^0.5.1",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.0",
"typescript": "^3.3.3"
}
}
npm run build will build the ZIP file.
npm run deploy will build the ZIP file and deploy it to Azure.
For complete information check Azure Function app with Terraform.

Jest: Cannot use import statement outside a module

I got an error when I run test using Jest, I tried to fix this error for 2 hours. But, I couldn't fix it. My module is using gapi-script package and error is occurred in this package. However, I don't know why this is occurred and how to fix it.
jest.config.js
module.exports = {
"collectCoverage": true,
"rootDir": "./",
"testRegex": "__tests__/.+\\.test\\.js",
"transform": {
'^.+\\.js?$': "babel-jest"
},
"moduleFileExtensions": ["js"],
"moduleDirectories": [
"node_modules",
"lib"
]
}
babel.config.js
module.exports = {
presets: [
'#babel/preset-env',
]
};
methods.test.js
import methods, { typeToActions } from '../lib/methods';
methods.js
import { gapi } from "gapi-script";
...
Error Message
C:\haram\github\react-youtube-data-api\node_modules\gapi-script\index.js:1
({"Object.":function(module,exports,require,__dirname,__filename,global,jest){import
{ gapi, gapiComplete } from './gapiScript';
SyntaxError: Cannot use import statement outside a module
What is wrong with my setting?
As of this writing, Jest is in the process of providing support for ES6 modules. You can track the progress here:
https://jestjs.io/docs/en/ecmascript-modules
For now, you can eliminate this error by running this command:
node --experimental-vm-modules node_modules/.bin/jest
instead of simply:
jest
Be sure to check the link before using this solution.
I solved this with the help of Paulo Coghi's answer to another question -
Does Jest support ES6 import/export?
Step 1:
Add your test environment to .babelrc in the root of your project:
{
"env": {
"test": {
"plugins": ["#babel/plugin-transform-modules-commonjs"]
}
}
}
Step 2:
Install the ECMAScript 6 transform plugin:
npm install --save-dev #babel/plugin-transform-modules-commonjs
Jest needs babel to work with modules.
For the testing alone, you do not need jest.config.js, just name the testfiles xxx.spec.js or xxx.test.js or put the files in a folder named test.
I use this babel.config.js:
module.exports = function (api) {
api.cache(true)
const presets = [
"#babel/preset-env"
]
return {
presets
}
}
Adding "type": "module" in package.json or using mjs as stated in other answers is not necessary when your setup is not too complicated.
I have also faced the same issue and this was resolved by adding following command-line option as a environment variable.
export NODE_OPTIONS=--experimental-vm-modules npx jest //linux
setx NODE_OPTIONS "--experimental-vm-modules npx jest" //windows
Upgrading Jest (package.json) to version 29 (latest as of now) solved this problem for me.

How to correctly bundle a vscode extension using webpack

The problem that i am having is that when i run vsce package i still get the This extension consists of 3587 separate files. For performance reasons, you should bundle your extension: warning, i followed the Bundling Extension steps, debugging works as expected.
package.json
{
"main": "./out/extension",
"scripts": {
"vscode:prepublish": "webpack --mode production",
"webpack": "webpack --mode development",
"webpack-dev": "webpack --mode development --watch",
"compile": "npm run webpack",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install"
},
}
The webpack config is an exact copy of the Bundling Extension example.
This sounds like you might've forgotten to add the source directories to .vscodeignore, so they're still being packaged into the release. The ignore file should probably contain at least the following, plus anything else not needed at runtime:
src/**
node_modules/**
If you are working with a Language Server extension which has both client and server folders, If you exclude the node_modules of the client and server from the bundle the extension would fail when installed and launch for the first time
.vscodeignore contains
.vscode
**/*.ts
**/*.map
out/**
node_modules/**
test_files/**
client/src/**
server/src/**
tsconfig.json
webpack.config.js
.gitignore
Also the documentation is a bit obsolete regarding the webpack.config.js, you have to wrap the 'use strict' into a function with all the settings.
The entry setting was changed according to my needs
//#ts-check
(function () {
'use strict';
const path = require('path');
/**#type {import('webpack').Configuration}*/
const config = {
target: 'node', // vscode extensions run in a Node.js-context πŸ“– -> https://webpack.js.org/configuration/node/
entry: './client/src/extension.ts', // the entry point of this extension, πŸ“– -> https://webpack.js.org/configuration/entry-context/
output: {
// the bundle is stored in the 'dist' folder (check package.json), πŸ“– -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, 'dist'),
filename: 'extension.js',
clean: true, //clean the dist folder for each time webpack is run
libraryTarget: 'commonjs2',
devtoolModuleFilenameTemplate: '../[resource-path]'
},
devtool: 'source-map',
externals: {
vscode: 'commonjs vscode' // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, πŸ“– -> https://webpack.js.org/configuration/externals/
},
resolve: {
// support reading TypeScript and JavaScript files, πŸ“– -> https://github.com/TypeStrong/ts-loader
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader'
}
]
}
]
}
};
module.exports = config;
}());

JSDoc output inconsistent between gulp-jsdoc & standard CLI

I'm trying to build docs for a simple set of JS code (given below). If I use gulp, the docs are created how I would expect them. If I use the CLI, the docs are incomplete.
Here's my JS code:
// BASE.js
/** #module BASE */
var BASE = {};
// MOD1.js
/** #class MOD1 - Test module */
BASE.MOD1 = Object.create({});
/**
* Just a test function
* #param {Object} var1 - A test variable
*/
BASE.MOD1.testFunction = function(var1){
alert('hi');
};
My gulp file:
var gulp = require('gulp'),
jsdoc = require('gulp-jsdoc'),
outDir = './gulp-docs/',
docInfo = {},
docOptions = {},
docTemplate = {},
srcFiles = [
"BASE.js",
"MOD1.js"
];
gulp.task('default', function() {
return gulp.src(srcFiles)
.pipe(jsdoc.parser(docInfo))
.pipe(jsdoc.generator(outDir, docTemplate, docOptions))
});
And my command line:
C:\DocTest> jsdoc BASE.js MOD1.js --configure rawconf.json --destination raw-docs
rawconf.json:
{
"tags": {
"allowUnknownTags": true
},
"plugins": [],
"templates": {},
"opts": {
"package": "./rawpackage.json"
}
}
rawpackage.json:
{}
I run both gulp and the jsdoc command from the Node.js command prompt.
Output from gulp is the following files:
BASE.js.html
BASE.MOD1.html
index.html
MOD1.js.html
module-BASE.html
Output from the CLI is the following files:
BASE.js.html
index.html
MOD1.js.html
module-BASE.html
module-BASE-BASE.MOD1.html
There are some small differences which I can chalk up to the differences between the gulp-jsoc version of jsdoc (3.3.0-alpha5) and the current version (3.3.0-beta3).
But the biggest difference is that while in the gulp output, I can find information on testFunction, there is no information to be found at all regarding testFunction anywhere in the CLI output. I've even searched the HTML code--nothing.
So did I do something wrong? I'm just trying to achieve parity at this point, and I've exhausted any documentation I could find online.
If you look at the gulp-jsdoc github page here, there's a "Big Fat Warning" that this plugin isn't being kept up to date.
Try using the gulp-shell plugin. You can use exactly what you typed into the command line.