Untitled

mail@pastecode.io avatar
unknown
javascript
2 years ago
2.3 kB
1
Indexable
const fs = require('fs');
const { exec } = require('child_process');

// fs.copyFile('./package.json', './build/package.json', (err) => {
//   if (err) throw err;
//   // eslint-disable-next-line no-console
//   console.log('package.json has been copied');
// });

const packageJsonPath = './package.json';
const packageJson = require('./package.json');

// delete packageJson.scripts['publish:pre'];
// delete packageJson.scripts['publish:build'];
// delete packageJson.scripts['publish:post'];
// delete packageJson.scripts['publish:package'];
// delete packageJson.scripts['publish:package:test'];

// fs.writeFile(
//   packageJsonPath,
//   JSON.stringify(packageJson, null, 2),
//   function (err, data) {
//     // eslint-disable-next-line no-console
//     if (err) return console.log(err);
//     // eslint-disable-next-line no-console
//     console.log('writing to ' + packageJsonPath);
//   }
// );

const preparePackageJson = async () => {
  delete packageJson.scripts['main'];
  packageJson.main = 'dist/main.js';
  packageJson.module = 'dist/module.js';
  packageJson.types = 'dist/types.d.ts';
  packageJson.files = ['dist'];
  fs.writeFileSync(
    packageJsonPath,
    JSON.stringify(packageJson, null, 2),
    function (err, data) {
      // eslint-disable-next-line no-console
      if (err) return console.log(err);
      // eslint-disable-next-line no-console
      console.log('writing to ' + packageJsonPath);
    }
  );
};

const restorePackageJson = async (packageJson) => {
  delete packageJson['main'];
  packageJson.main = 'src/';
  delete packageJson['module'];
  delete packageJson['types'];
  delete packageJson['files'];

  fs.writeFileSync(
    packageJsonPath,
    JSON.stringify(packageJson, null, 2),
    function (err, data) {
      // eslint-disable-next-line no-console
      if (err) return console.log(err);
      // eslint-disable-next-line no-console
      console.log('restoring ' + packageJsonPath);
    }
  );
};

(async () => {
  await preparePackageJson();
  exec('pnpm build', (error, stdout, stderr) => {
    if (error) {
      console.log(`error: ${error.message}`);
      return;
    }
    if (stderr) {
      console.log(`stderr: ${stderr}`);
      return;
    }
    console.log(`stdout: ${stdout}`);
  });
  await restorePackageJson();
})();