Skip to content

Commit

Permalink
Feature/web support (#174)
Browse files Browse the repository at this point in the history
* pack web segment

* default to npm for unpack if bun is set as default

* Update packer.js

* Update packer.js

* disable bun in CLI

* bump version

---------

Co-authored-by: Definitely Not Vlad <[email protected]>
  • Loading branch information
sstimac and Definitely-Not-Vlad authored Sep 3, 2024
1 parent 3f08339 commit 6766d92
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shoutem/cli",
"version": "0.15.2",
"version": "0.16.0",
"description": "Command-line tools for Shoutem applications",
"repository": {
"type": "git",
Expand Down
7 changes: 6 additions & 1 deletion src/clients/default-package-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ export function setDefaultPackageManager(name) {

export function getDefaultPackageManager() {
try {
return fs.readFileSync(defaultPackageManagerFilePath, 'utf8');
// Disable bun in favour of npm for the time being
const resolvedManager = fs.readFileSync(
defaultPackageManagerFilePath,
'utf8',
);
return resolvedManager === 'bun' ? 'npm' : resolvedManager;
} catch (err) {
setDefaultPackageManager('npm');
return 'npm';
Expand Down
43 changes: 38 additions & 5 deletions src/services/packer.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,26 @@ function hasPackageJson(dir) {
}

async function packageManagerPack(dir, destinationDir) {
const resultFilename = path.join(destinationDir, `${path.basename(dir)}.tgz`);
const packageJsonPath = path.join(dir, 'package.json');
const component = path.basename(dir);
const resultFilename = path.join(destinationDir, `${component}.tgz`);
const isWeb = component === 'web';
const appDir = isWeb ? dir.replace('web', 'app') : dir;

const packageJsonPath = path.join(appDir, 'package.json');

const originalFileContent = await fs.readFile(packageJsonPath);
const packageJson = await readJsonFile(packageJsonPath);

const timestamp = new Date().getTime();
packageJson.version = `${packageJson.version}-build${timestamp}`;
packageJson.dependencies = isWeb
? packageJson.webDependencies
: packageJson.dependencies;

await writeJsonFile(packageJson, packageJsonPath);
const { stdout } = await exec(`${packageManager} pack`, { cwd: dir });
const { stdout } = await exec(`${packageManager} pack`, { cwd: appDir });
const packageFilename = stdout.replace(/\n$/, '');
const packagePath = path.join(dir, packageFilename);
const packagePath = path.join(appDir, packageFilename);

await mv(packagePath, resultFilename);

Expand Down Expand Up @@ -104,6 +111,18 @@ function hasExtensionsJson(dir) {
return pathExists(path.join(dir, 'extension.json'));
}

async function hasWebDependencies(dir) {
const packageJsonPath = path.join(dir, 'app', 'package.json');

const packageJson = await readJsonFile(packageJsonPath);

if (!packageJson.webDependencies) {
return false;
}

return true;
}

function hasCloudComponent(dir) {
return hasPackageJson(path.join(dir, 'cloud'));
}
Expand Down Expand Up @@ -163,6 +182,12 @@ export default async function shoutemPack(dir, options) {
components.push('cloud');
}

const hasWeb = await hasWebDependencies(dir);

if (hasWeb) {
components.push('web');
}

const packedDirectories = components.map(d => path.join(dir, d));

if (!(await hasExtensionsJson(dir))) {
Expand All @@ -177,7 +202,15 @@ export default async function shoutemPack(dir, options) {
const packageDir = path.join(tmpDir, 'package');
await fs.mkdir(packageDir);

const dirsToPack = await Promise.filter(packedDirectories, hasPackageJson);
const filteredDirsToPack = await Promise.filter(
packedDirectories,
hasPackageJson,
);
// We still want to pack the web segment even though it uses package.json
// from the app segment
const dirsToPack = hasWeb
? [...filteredDirsToPack, path.join(dir, 'web')]
: filteredDirsToPack;

if (options.nobuild) {
console.error('Skipping build step due to --nobuild flag.');
Expand Down

0 comments on commit 6766d92

Please sign in to comment.