Skip to content

Commit

Permalink
Support starting both remote and local paths
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Dec 25, 2020
1 parent 7e096e6 commit 437814e
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 44 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,23 @@ You are done. Time to hack on your extension!

### Kickstart any sample from `chrome-extesions-sample`

Say you like the extension sample `set_icon_path`. The sample URL on GitHub is https://github.com/GoogleChrome/chrome-extensions-samples/tree/master/api/browserAction/set_icon_path.
Say you like the extension sample `set_icon_path` from [chrome-extesions-sample](https://github.com/GoogleChrome/chrome-extensions-samples/). Its URL on GitHub is https://github.com/GoogleChrome/chrome-extensions-samples/tree/master/api/browserAction/set_icon_path.

```sh
npx extension-create start --remote https://github.com/GoogleChrome/chrome-extensions-samples/tree/master/api/browserAction/set_icon_path
npx extension-create start https://github.com/GoogleChrome/chrome-extensions-samples/tree/master/api/browserAction/set_icon_path
```

Will not only download the extension but also kickstart a Chrome instance in a fresh profile with your sample extension loaded. Try it!
Will not only download the extension but also kickstart a Chrome instance in a fresh profile with your sample extension loaded. Try it yourself!

This also works with any other GitHub URL
as long as it points to a directory includig the manifest within its root path.
> **Note:** It also works with any other GitHub URL, as long as it points to a directory with an available manifest file.
## Next steps

Project roadmap TBD. These are the major areas I plan to cover (sorted)

- [x] `create` - Everything needed to create a new extension. Supports templates (alpha)
- [ ] `develop` - Everything needed to develop a new extension. (under development)
- [x] `start` - Runs the extension on a browser with support for hot-reload, JS modules, and custom browser configs. (in progress)
- [x] `start` - Runs the extension on a browser with support for hot-reload, JS modules, and custom browser configs. (alpha)
- [ ] `--browser` - Sets the browser to open. Defaults to default broser
- [x] `chrome` - Develop your extension running a standalone Chrome instance
- [ ] `firefox` - Develop your extension running a standalone Firefox instance
Expand Down
15 changes: 4 additions & 11 deletions develop/start/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,17 @@ function startExtensionCLI (clientProgram = program) {
clientProgram
.version(packageJson.version)
.command('start')
.usage('start [options]')
.usage('start [path-to-extension-folder]')
.description('start the development server')
.option(
'-m, --manifest <path-to-manifest-file>',
'use this if your manifest file doesn\'t live in the root folder'
)
.option(
'-r, --remote <path-to-remote-url>',
'download and run a remote extension hosted on GitHub'
)

.on('--help', () => messages.programHelp())
.parse(process.argv)

const projectDir = process.cwd()
const commands = clientProgram.commands[0]
const { remote, manifest } = commands
const [, customPath] = commands.args

startExtension(projectDir, { remote, manifest })
startExtension(projectDir, { customPath })
}

// If the module was called from the cmd line, execute it
Expand Down
14 changes: 7 additions & 7 deletions develop/start/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ async function createTmpExtension () {
}

describe('`start` command line interface', () => {
it.todo('calls browser with a resolved manifest path')
it.todo('warns users if manifest file is missing')

describe('-m, --manifest', () => {
it.todo('warns users if manifest flag is empty')
it.todo('starts an extension with a custom manifest file path')
})
it.todo('starts extension from local path with arguments')
it.todo('starts extension from local path without arguments')
it.todo('starts extension from local path - manifest in src/')
it.todo('starts extension from local path - manifest in public/')
it.todo('starts extension from remote (URL) path')
it.todo('starts extension from remote (URL) path - manifest in src/')
it.todo('starts extension from remote (URL) path - manifest in public/')
})
3 changes: 3 additions & 0 deletions develop/start/config/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ module.exports = (projectDir, manifestPath) => {
extensionPath: projectDir
})
],
resolve: {
extensions: ['js', '.json']
},
module: {
// Adapted from
// https://github.com/webextension-toolbox/webextension-toolbox
Expand Down
9 changes: 5 additions & 4 deletions develop/start/messages/manifestNotFound.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ module.exports = function () {
log(`
# Error! Can't find the project's manifest file.
By default both root and \`public/\` folder are scanned, but none was found.
By default, extension-create scans the root folder and the paths to
\`src/\`, and \`public/\` looking for a manifest file, but none was found.
If you store your manifest file somewhere else, you need to tell
\`extension-create\` where to look using the \`--manifest\` flag.
The argument after \`start\` needs to point to a folder where the
manifest is available within one of the paths above.
\`extension-create start --manifest=\`<path-to-manifest>
\`extension-create start\` <path-to-extension-folder>
`)
}
44 changes: 37 additions & 7 deletions develop/start/startExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,54 @@
// ╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝

const path = require('path')
const fs = require('fs-extra')

const { log } = require('log-md')
const githubPartials = require('github-partials')

const resoleManifest = require('./steps/resolveManifest')
const startWebpack = require('./steps/startWebpack')

module.exports = async function (projectDir, { manifest, remote }) {
let currentProjectDir = projectDir
function setCurrentProjectDirFromRemote (customPath) {
if (new URL(customPath).hostname !== 'github.com') {
log(`
The remote extension URL must be stored on GitHub.
`)
process.exit(1)
}

githubPartials(customPath)
return path.join(process.cwd(), path.basename(customPath))
}

async function setCurrentProjectDirFromLocal (customPath) {
const extensionPath = await fs.stat(customPath)

if (!extensionPath.isDirectory()) {
log(`
The local extension path must be a directory.
`)
process.exit(1)
}

return customPath
}

module.exports = async function (projectDir, { customPath }) {
let currentProjectDir

// console.log('👾👾👾👾👾👾👾👾👾👾👾👾', )
try {
if (remote) {
githubPartials(remote)
currentProjectDir = path.join(process.cwd(), path.basename(remote))
if (!customPath) {
currentProjectDir = await setCurrentProjectDirFromLocal(customPath)
} else if (customPath.startsWith('http')) {
currentProjectDir = setCurrentProjectDirFromRemote(customPath)
} else {
// No user arguments, default to cwd
currentProjectDir = path.resolve(projectDir, customPath)
}

const resolvedManifest = await resoleManifest(currentProjectDir, manifest)

const resolvedManifest = await resoleManifest(currentProjectDir, customPath)
startWebpack(currentProjectDir, resolvedManifest)
} catch (error) {
log(`
Expand Down
17 changes: 8 additions & 9 deletions develop/start/steps/resolveManifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,31 @@ const fs = require('fs-extra')

const message = require('../messages')

module.exports = async function (workingDir, manifestFile) {
// Defaults to user-defined path
let manifestFilePath = manifestFile
module.exports = async function (workingDir) {
let manifestFilePath

// Iterate over common paths looking for the manifest file.
try {
// Try manifest path provided by user
await fs.access(manifestFilePath)
// Start from usual suspects, check src/
await fs.access(path.join(workingDir, 'src', 'manifest.json'))

return manifestFilePath
} catch (error) {
try {
// User didn't provide a manifest file, check in public/
// Check in public/
manifestFilePath = path.join(workingDir, 'public', 'manifest.json')
await fs.access(manifestFilePath)

return manifestFilePath
} catch (error) {
try {
// Nothing found in public/, try the root directory
// Check the root directory
manifestFilePath = path.join(workingDir, 'manifest.json')
await fs.access(manifestFilePath)

return manifestFilePath
} catch (error) {
// Nothing found neither in public/ or path, and user did not
// provide a --manifest option. Manifests are required so we exit.
// Nothing found. Manifests are required so we exit.
message.manifestNotFound()
process.exit(1)
}
Expand Down

0 comments on commit 437814e

Please sign in to comment.