Skip to content

Commit

Permalink
JS version of install.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
lithrel committed Mar 6, 2023
1 parent 6e6f0e0 commit 5abd0ab
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14
14
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,26 @@
# Resources

- https://github.com/WordPress/developer-blog-content/issues/89

# To do

## Perfs
- Add Redis server to check if instance is faster that way

## Permissions
- add fix_permissions function
- check why everything is root (missing user with UID used by wp-env ?)

## NRO
- Full dev install
- Clone dev repo, merge composer, install
- Fetch DB, import and switch

## Team
- Pull & activate child-theme individually

## Functions
- get_x_from_repo
- get_x_from_release
- build_assets
- import DB
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"devDependencies": {
},
"scripts": {
"env:install": "./scripts/install.sh",
"env:install": "node ./scripts/install.js",
"env:clean": "./scripts/clean.sh",
"env:nro-install": "",
"env:theme-install": "",
Expand Down
108 changes: 108 additions & 0 deletions scripts/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const { execSync } = require('child_process');
const { renameSync, readFileSync, writeFileSync, existsSync, lstatSync, mkdirSync, copyFileSync } = require('fs');
const { run } = require('./lib/run');
const { getMainReposFromGit } = require('./lib/get-main-repos');
const { mysqlRootExec, mysqlRootExecNoTTY, mysqlRootCommand, mysqlRootCommandNoTTY } = require('./lib/mysql');

const wpEnvConfig = JSON.parse(readFileSync('./.wp-env.json'));
const nodeVersionRequired = parseInt(readFileSync('.nvmrc'));
const config = {
appDir: 'planet4',
nodeVersion: nodeVersionRequired,
...wpEnvConfig
};
console.log(process.cwd(), config);
const baseDir = `${config.appDir}/planet4-base`;
const themeDir = config.mappings['wp-content/themes'];
const pluginDir = config.mappings['wp-content/plugins'];

/**
* Node version control
*/
console.log('Node version: ' + process.version);
const nodeVersionRunning = parseInt(process.version.split('.')[0].substr(1)); // <string> "vX.Y.Z" => <int> X
if (nodeVersionRunning !== nodeVersionRequired) {
console.error(`This install requires Node version ${nodeVersionRequired}. You can run <nvm use> to switch version.`);
process.exit(1);
}

/**
* Install main repos
*/
console.log('Cloning base repo ...');
console.log(`>>> ${baseDir}`);
if (existsSync(`${baseDir}`) && lstatSync(`${baseDir}`).isDirectory()) {
run('git status', {cwd: `${baseDir}`});
} else {
run(`git clone [email protected]:greenpeace/planet4-base.git ${baseDir}`);
}

console.log('Cloning main repos ...');
getMainReposFromGit({themeDir, pluginDir});

/**
* Start WP
*/
run('wp-env start --update');

/**
* Merge composer requirements
*/
console.log('Merging composer requirements ...');
copyFileSync(`${baseDir}/composer.json`, `${config.appDir}/composer.json`);
run(`wp-env run composer -d /app/${config.appDir}/ config --unset repositories.0`);
run(`wp-env run composer -d /app/${config.appDir}/ config --unset extra.merge-plugin`);
run(`wp-env run composer -d /app/${config.appDir}/ config --json extra.installer-paths '"{\\"plugins/{\\$name}/\\": [\\"type:wordpress-plugin\\"],\\"themes/{\\$name}/\\": [\\"type:wordpress-theme\\"]}"'`);
run(`wp-env run composer -d /app/${config.appDir}/ config platform.php "${config.phpVersion}"`);
if (existsSync(`${themeDir}/planet4-master-theme`)
&& lstatSync(`${themeDir}/planet4-master-theme`).isDirectory()
) {
run('wp-env run composer -d /app/planet4/ remove --no-update greenpeace/planet4-master-theme');
}
if (existsSync(`${pluginDir}/planet4-plugin-gutenberg-blocks`)
&& lstatSync(`${pluginDir}/planet4-plugin-gutenberg-blocks`).isDirectory()
) {
run('wp-env run composer -d /app/planet4/ remove --no-update greenpeace/planet4-plugin-gutenberg-blocks');
}

/**
* Install themes/plugins
*/
console.log('Installing themes and plugins ...');
run(`wp-env run composer -d /app/${config.appDir}/ install --ignore-platform-reqs`);
run(`wp-env run composer -d /app/${themeDir}/planet4-master-theme config platform.php "${config.phpVersion}"`);
run(`wp-env run composer -d /app/${themeDir}/planet4-master-theme install --ignore-platform-reqs`);
run(`wp-env run composer -d /app/${pluginDir}/planet4-plugin-gutenberg-blocks config platform.php "${config.phpVersion}"`);
run(`wp-env run composer -d /app/${pluginDir}/planet4-plugin-gutenberg-blocks install --ignore-platform-reqs`);

if (!existsSync(`${themeDir}/planet4-master-theme/assets/build`)) {
console.log('Generating assets ...');
process.env.PUPPETEER_SKIP_DOWNLOAD = true;
process.env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true;
run('npm install && npm run build', {cwd: `${themeDir}/planet4-master-theme`});
run('npm install && npm run build', {cwd: `${pluginDir}/planet4-plugin-gutenberg-blocks`});
}

run('wp-env run cli plugin activate --all');

/**
* Database
*/
if (!existsSync('content')) {
mkdirSync('content');
}
run('curl --fail https://storage.googleapis.com/planet4-default-content/planet4-defaultcontent_wordpress-v0.2.13.sql.gz > content/planet4-defaultcontent_wordpress-v0.2.13.sql.gz');

const dbName = 'planet4_dev';
mysqlRootExec(`-e \\
"CREATE DATABASE IF NOT EXISTS planet4_dev; \\
GRANT all privileges on ${dbName}.* to 'root'@'%'; \\
USE ${dbName};"`);

mysqlRootExec('-e \'SET GLOBAL max_allowed_packet=16777216\'');
run(`zcat < "content/planet4-defaultcontent_wordpress-v0.2.13.sql.gz" | ${mysqlRootCommandNoTTY(dbName)}`);
// Fix GTID_PURGED value issue
// mysql_root_exec -D planet4_dev -e 'RESET MASTER'

run(`wp-env run cli config set DB_NAME ${dbName}`);
run('wp-env run cli user update admin --user_pass=admin --role=administrator');
30 changes: 30 additions & 0 deletions scripts/lib/get-main-repos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

const { execSync } = require( 'child_process' );
const { renameSync, existsSync, lstatSync } = require('fs');

function getMainReposFromGit({themeDir, pluginDir}) {
console.log();
console.log(`>>> ${themeDir}/planet4-master-theme`);
if (existsSync(`${themeDir}/planet4-master-theme`) && lstatSync(`${themeDir}/planet4-master-theme`).isDirectory()) {
execSync('git status', {cwd: `${themeDir}/planet4-master-theme`, stdio: 'inherit'});
} else {
execSync(`git clone [email protected]:greenpeace/planet4-master-theme.git ${themeDir}/planet4-master-theme`, {stdio: 'inherit'});
}

console.log();
console.log(`>>> ${pluginDir}/planet4-plugin-gutenberg-blocks`);
if (existsSync(`${pluginDir}/planet4-plugin-gutenberg-blocks`) && lstatSync(`${pluginDir}/planet4-plugin-gutenberg-blocks`).isDirectory()) {
execSync('git status', {cwd: `${pluginDir}/planet4-plugin-gutenberg-blocks`, stdio: 'inherit'});
} else {
execSync(`git clone --recurse-submodules --shallow-submodule [email protected]:greenpeace/planet4-plugin-gutenberg-blocks.git ${pluginDir}/planet4-plugin-gutenberg-blocks`, {stdio: 'inherit'});
}
};

function getMainReposFromRelease({themeDir, pluginDir}) {
console.error('@todo implement');
}

module.exports = {
getMainReposFromGit,
getMainReposFromRelease,
};
29 changes: 29 additions & 0 deletions scripts/lib/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { execSync } = require('child_process');

function mysqlRootExec(cmd) {
const dcCommand = mysqlRootCommand(cmd);
console.log(dcCommand);
return execSync(dcCommand, {stdio: 'inherit'});
};

function mysqlRootExecNoTTY(cmd) {
const dcCommand = mysqlRootCommandNoTTY(cmd);
console.log(dcCommand);
return execSync(dcCommand, {stdio: 'inherit'});
};

function mysqlRootCommand(cmd) {
return `docker-compose -f \$(wp-env install-path)/docker-compose.yml exec mysql mysql -uroot -ppassword ${cmd}`;
};

function mysqlRootCommandNoTTY(cmd) {
return `docker-compose -f \$(wp-env install-path)/docker-compose.yml exec -T mysql mysql -uroot -ppassword ${cmd}`;
};

module.exports = {
mysqlRootExec,
mysqlRootExecNoTTY,
mysqlRootCommand,
mysqlRootCommandNoTTY
}

7 changes: 7 additions & 0 deletions scripts/lib/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { execSync } = require('child_process');

function run(cmd, opts) {
return execSync(cmd, {stdio: 'inherit', ...opts});
}

module.exports = { run };

0 comments on commit 5abd0ab

Please sign in to comment.