Skip to content

Commit

Permalink
add sodium extension preinstall for m1 macs
Browse files Browse the repository at this point in the history
  • Loading branch information
ejnshtein committed Jun 2, 2022
1 parent b83d3da commit 095c4a3
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
16 changes: 16 additions & 0 deletions build-packages/magento-scripts/lib/config/cma-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Conf = require('conf');

const pkg = require('../../package.json');
/**
* @type {import('conf/dist/source').default}
*/
const cmaGlobalConfig = new Conf({
configName: 'config',
projectName: 'create-magento-app',
projectVersion: pkg.version,
defaults: {}
});

module.exports = {
cmaGlobalConfig
};
93 changes: 93 additions & 0 deletions build-packages/magento-scripts/lib/tasks/php/install-sodium.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const path = require('path');
const os = require('os');
const logger = require('@scandipwa/scandipwa-dev-utils/logger');
const { cmaGlobalConfig } = require('../../config/cma-config');
const downloadFile = require('../../util/download-file');
const { execCommandTask } = require('../../util/exec-async-command');
const { getEnabledExtensions } = require('./extensions');

const libsodiumArchiveUrl = 'https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable.tar.gz';

const HAS_LIBSODIUM_BEEN_INSTALLED = 'hasLibsodiumBeenInstalled';

/**
* @returns {import('listr2').ListrTask<import('../../../typings/context').ListrContext>}
*/
const installSodiumExtension = () => ({
skip: async (ctx) => {
if (ctx.isArmMac) {
if (cmaGlobalConfig.has(HAS_LIBSODIUM_BEEN_INSTALLED)) {
const hasLibsodiumBeenInstalled = cmaGlobalConfig.get(HAS_LIBSODIUM_BEEN_INSTALLED);
return hasLibsodiumBeenInstalled;
}

return false;
}

return true;
},
task: async (ctx, task) => {
task.title = 'Preparing libsodium to installation';
const tempDir = os.tmpdir();
const destination = path.join(tempDir, path.parse(libsodiumArchiveUrl).base);
const extractedArchivePath = path.join(tempDir, 'libsodium-stable');
const enabledExtensions = await getEnabledExtensions(ctx.config);

if ('sodium' in enabledExtensions) {
cmaGlobalConfig.set(HAS_LIBSODIUM_BEEN_INSTALLED, true);
task.skip();
return;
}

return task.newListr([
{
title: 'Downloading archive',
task: async () => {
await downloadFile(libsodiumArchiveUrl, { destination });
}
},
{
...execCommandTask(`tar -zxf ${destination}`, {
cwd: tempDir
}),
title: 'Extracting archive'
},
execCommandTask('./configure', {
cwd: extractedArchivePath
}),
execCommandTask('make && make check', {
cwd: extractedArchivePath
}),
{
title: 'Installing...',
task: async (ctx, task) => {
task.output = 'Enter your sudo password!';
task.output = logger.style.command(`>[sudo] password for ${ os.userInfo().username }:`);

return task.newListr(
execCommandTask('sudo make install', {
callback: (t) => {
task.output = t;
},
pipeInput: true,
cwd: extractedArchivePath
})
);
},
options: {
bottomBar: 10
}
},
{
task: () => {
cmaGlobalConfig.set(HAS_LIBSODIUM_BEEN_INSTALLED, true);
}
}
]);
},
options: {
bottomBar: 10
}
});

module.exports = installSodiumExtension;
2 changes: 2 additions & 0 deletions build-packages/magento-scripts/lib/tasks/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const getIsWsl = require('../util/is-wsl');
const checkForXDGOpen = require('../util/xdg-open-exists');
const { getInstanceMetadata, constants: { WEB_LOCATION_TITLE } } = require('../util/instance-metadata');
const validatePHPInstallation = require('./php/validate-php');
const installSodiumExtension = require('./php/install-sodium');

/**
* @type {() => import('listr2').ListrTask<import('../../typings/context').ListrContext>}
Expand Down Expand Up @@ -113,6 +114,7 @@ const configureProject = () => ({
exitOnError: true
})
},
installSodiumExtension(),
configurePhp(),
validatePHPInstallation(),
installPrestissimo(),
Expand Down

0 comments on commit 095c4a3

Please sign in to comment.