Created
February 7, 2022 14:13
-
-
Save lifeart/5ef68bae3f6f3922bd19bf65349d4d2d to your computer and use it in GitHub Desktop.
v2 addon migrator
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
import path from 'path'; | |
import os from 'os'; | |
import fs from 'fs-extra'; | |
// https://github.com/embroider-build/embroider/blob/main/PORTING-ADDONS-TO-V2.md | |
async function migrate(entry) { | |
const { default: walkSync } = await import('walk-sync'); | |
const files = () => walkSync(entry, { | |
ignore: [ | |
'.git', | |
'.github', | |
'node_modules' | |
] | |
}); | |
const rm = (fName) => { | |
try { | |
fs.unlinkSync(path.join(entry, fName)); | |
} catch (e) { | |
console.info(`${fName} is not removed.`); | |
} | |
}; | |
const read = (fName) => fs.readFileSync(path.join(entry, fName), 'utf8'); | |
// const read = (fName) => fs.readFileSync(path.join(entry, fName), 'utf-8'); | |
const mkdir = (dirName) => { | |
const p = path.join(entry, dirName); | |
if (!fs.existsSync(p)) { | |
fs.mkdirSync(path.join(entry, dirName)) | |
} | |
}; | |
const move = (files, dir) => { | |
files.forEach(file => { | |
const from = path.join(entry, file); | |
const to = path.join(entry, dir, file); | |
if (fs.existsSync(from) && !fs.existsSync(to)) { | |
fs.moveSync(path.join(entry, file), path.join(entry, dir, file)); | |
} else if (fs.existsSync(to)) { | |
rm(file); | |
} | |
}) | |
} | |
const create = (fName, content) => { | |
fs.writeFileSync(path.join(entry, fName), content, 'utf-8'); | |
} | |
const rename = (from, to) => { | |
fs.moveSync(path.join(entry, from), path.join(entry, to)); | |
} | |
const copyFile = (fName, fTarget) => { | |
fs.copyFileSync(path.join(entry, fName), path.join(entry, fTarget)); | |
} | |
function tryStep(number, fn) { | |
// eslint-disable-next-line no-useless-catch | |
try { | |
fn(); | |
} catch (e) { | |
//console.log(e); | |
//console.info(`Unable to perform step ${number}`); | |
throw e; | |
} | |
} | |
tryStep(1, () => { | |
rm(`yarn.lock`); | |
}); | |
tryStep(2, () => { | |
mkdir('test-app'); | |
mkdir('_addon'); | |
}); | |
tryStep(3, () => { | |
move(['addon', 'addon-test-support', 'app', 'blueprints', 'config/environment.js', 'index.js'], '_addon'); | |
}); | |
tryStep(4, () => { | |
// rm('addon'); | |
rename('_addon', 'addon'); | |
}); | |
tryStep(5, () => { | |
files().forEach(f => { | |
if (!f.endsWith('.md') && !f.includes(path.sep)) { | |
move(f, 'test-app'); | |
} | |
}); | |
}); | |
tryStep(6, () => { | |
move(['test-app/tests/dummy'], 'test-app'); | |
}); | |
tryStep(7, () => { | |
create('package.json', JSON.stringify({ | |
private: true, | |
workspaces: [ | |
"addon", | |
"test-app" | |
] | |
})); | |
}); | |
tryStep(8, () => { | |
create('.gitignore', ` | |
# you definitely want this: | |
*/node_modules/ | |
# and you can put in anything else that tends to accumulate in your environment: | |
yarn-error.log | |
.DS_Store | |
`.trim()); | |
}); | |
tryStep(9, () => { | |
copyFile('test-app/package.json', 'addon/package.json'); | |
}); | |
tryStep(10, () => { | |
const content = JSON.parse(read('addon/package.json')); | |
delete content.scripts; | |
delete content.devDependencies; | |
delete content['ember-addon'].configPath; | |
create('addon/package.json', JSON.stringify(content, null, 2)); | |
}); | |
tryStep(11, () => { | |
const content = JSON.parse(read('test-app/package.json')); | |
Object.keys(content.dependencies).forEach((packageName) => { | |
content.devDependencies[packageName] = content[packageName]; | |
}) | |
delete content.dependencies; | |
create('test-app/package.json', JSON.stringify(content, null, 2)); | |
}); | |
tryStep(12, () => { | |
const content = JSON.parse(read('test-app/package.json')); | |
const addonContent = JSON.parse(read('addon/package.json')); | |
content.devDependencies[addonContent.name] = addonContent.version; | |
create('test-app/package.json', JSON.stringify(content, null, 2)); | |
}); | |
tryStep(13, () => { | |
const content = JSON.parse(read('test-app/package.json')); | |
content.name = 'test-app'; | |
delete content['ember-addon']; | |
content.keywords = content.keywords.filter(key => key !== 'ember-addon'); | |
create('test-app/package.json', JSON.stringify(content, null, 2)); | |
}); | |
tryStep(15, () => { | |
const content = JSON.parse(read('test-app/ember-cli-build.js')); | |
const change1 = content.replace( | |
`const EmberAddon = require('ember-cli/lib/broccoli/ember-addon');`, | |
`const EmberApp = require('ember-cli/lib/broccoli/ember-app');` | |
); | |
const change2 = change1.replace( | |
`let app = new EmberAddon(defaults, {`, | |
`let app = new EmberApp(defaults, {` | |
) | |
create('test-app/ember-cli-build.js', change2); | |
}); | |
tryStep(16, () => { | |
// | |
}); | |
tryStep(19, () => { | |
rm('test-app/config/ember-cli-update.json'); | |
}); | |
} | |
const entry = path.join(os.homedir(), 'Documents', 'repos', 'ember-ref-bucket'); | |
migrate(entry); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment