Last active
October 13, 2018 00:21
-
-
Save umarov/470081be232486954565544c65c7f37e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path') | |
const { promisify } = require('util') | |
const fs = require('fs') | |
function getApps() { | |
return promisify(fs.readdir)('app') | |
} | |
const defaultManifest = { | |
bower: [], | |
vendor: [] | |
} | |
const componentsString = 'components/' | |
function fixStylePaths(stylePath) { | |
if (stylePath.startsWith('../../bower')) { | |
const newStylePath = stylePath.substring( | |
stylePath.indexOf(componentsString) + componentsString.length | |
) | |
return `../node_modules/${newStylePath}` | |
} | |
return stylePath | |
} | |
function buildNewManifestJson(name) { | |
const manifestJson = require(path.resolve('app', name, 'manifest.json')) | |
const newVendorPaths = Array.from( | |
new Set([ | |
...manifestJson.bower.map(packagePath => `../node_modules/${packagePath}`), | |
...manifestJson.vendor | |
]) | |
) | |
return Object.assign({}, defaultManifest, { | |
vendor: newVendorPaths, | |
styles: manifestJson.styles ? manifestJson.styles.map(fixStylePaths) : [] | |
}) | |
} | |
async function buildNewPackageJson(appName) { | |
const { dependencies, devDependencies, name, version } = require(path.resolve( | |
'app', | |
appName, | |
'bower.json' | |
)) | |
if (await promisify(fs.exists)(path.resolve('app', appName, 'package.json'))) { | |
const packageJson = require(path.resolve('app', appName, 'package.json')) | |
return Object.assign({}, { dependencies, devDependencies, name, version }, packageJson) | |
} else { | |
return { dependencies, devDependencies, name, version } | |
} | |
} | |
async function convertAppsToNpm() { | |
const apps = (await getApps()).filter(name => !name.startsWith('.')) | |
const appsWithContent = await Promise.all( | |
apps.map(async name => { | |
const content = await promisify(fs.readdir)(path.resolve('app', name)) | |
return { | |
name, | |
content | |
} | |
}) | |
) | |
const bowerApps = appsWithContent.filter(app => app.content.includes('bower.json')) | |
bowerApps.map(async ({ name }) => { | |
const newPackageJson = await buildNewPackageJson(name) | |
const newManifestJson = buildNewManifestJson(name) | |
await Promise.all([ | |
promisify(fs.writeFile)(path.resolve('app', name, 'manifest.json'), JSON.stringify(newManifestJson, null, 2) + '\n'), | |
promisify(fs.writeFile)(path.resolve('app', name, 'package.json'), JSON.stringify(newPackageJson, null, 2) + '\n') | |
]) | |
console.log(`Finished transitioning ${name} from bower to npm`) | |
await promisify(fs.unlink)(path.resolve('app', name, 'bower.json')) | |
console.log(`Removed bower.json from ${name}`) | |
}) | |
} | |
convertAppsToNpm() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment