create-near-app 3.0.0-pre.7.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -14,6 +14,9 @@ ncp.limit = 16
14
14
  const rustSetup = require('./utils/rust-setup')
15
15
  const mixpanel = require('./utils/tracking')
16
16
 
17
+ const type = os.type()
18
+ const arch = os.arch()
19
+
17
20
  const renameFile = async function (oldPath, newPath) {
18
21
  return new Promise((resolve, reject) => {
19
22
  fs.rename(oldPath, newPath, (err) => {
@@ -54,10 +57,13 @@ function copyDir(source, dest, { skip, veryVerbose } = {}) {
54
57
  }
55
58
 
56
59
  const createProject = async function ({ contract, frontend, projectDir, veryVerbose }) {
57
- if (os.platform() === 'win32') {
58
- console.log('Sorry, create-near-app is not compatible with Windows. Please consider using Windows Subsystem for Linux.\n')
60
+ const supports_sandbox = (type === 'Linux' || type === 'Darwin') && arch === 'x64'
61
+
62
+ if (!supports_sandbox && contract === 'assemblyscript') {
63
+ console.log('Sorry, assemblyscript is not supported in your system, use --contract=rust')
59
64
  return
60
65
  }
66
+
61
67
  // track used options
62
68
  mixpanel.track(frontend, contract)
63
69
 
@@ -72,19 +78,53 @@ const createProject = async function ({ contract, frontend, projectDir, veryVerb
72
78
  await copyDir(sourceTemplateDir, projectDir, { veryVerbose, skip: skip.map(f => path.join(sourceTemplateDir, f)) })
73
79
 
74
80
  // copy tests
75
- const sourceTestDir = __dirname + '/integration-tests'
76
- await copyDir(sourceTestDir, `${projectDir}/integration-tests`, { veryVerbose, skip: skip.map(f => path.join(sourceTestDir, f)) })
81
+ if (supports_sandbox) {
82
+ // Supports Sandbox
83
+ const sourceTestDir = __dirname + '/integration-tests'
84
+ await copyDir(sourceTestDir, `${projectDir}/integration-tests/`, { veryVerbose, skip: skip.map(f => path.join(sourceTestDir, f)) })
85
+ fs.rmSync(`${projectDir}/integration-tests/js`, {recursive: true})
86
+ }else{
87
+ // Others use simple ava testing
88
+ console.log('Our testing framework (workspaces) is not compatible with your system.\n')
89
+ console.log('Your project will default to basic JS testing.\n')
90
+ const sourceTestDir = __dirname + '/integration-tests/js'
91
+ await copyDir(sourceTestDir, `${projectDir}/integration-tests`, { veryVerbose, skip: skip.map(f => path.join(sourceTestDir, f)) })
92
+
93
+ await replaceInFiles({
94
+ files: `${projectDir}/package.json`,
95
+ from: '"test:integration:ts": "cd integration-tests/ts && npm run test"',
96
+ to: '"test:integration:ts": "echo not supported"'
97
+ })
98
+ await replaceInFiles({
99
+ files: `${projectDir}/package.json`,
100
+ from: '"test:integration:rs": "cd integration-tests/rs && cargo run --example integration-tests"',
101
+ to: '"test:integration:ts": "echo not supported"'
102
+ })
103
+ await replaceInFiles({
104
+ files: `${projectDir}/package.json`,
105
+ from: '"test:integration": "npm run test:integration:ts && npm run test:integration:rs"',
106
+ to: '"test:integration": "npm run deploy && cd integration-tests && npm run test"'
107
+ })
108
+ await replaceInFiles({
109
+ files: `${projectDir}/package.json`,
110
+ from: '"near-workspaces": "^2.0.0",',
111
+ to: ' '
112
+ })
113
+ }
77
114
 
78
115
  // copy contract files
79
116
  const contractSourceDir = `${__dirname}/contracts/${contract}`
80
117
  await copyDir(contractSourceDir, `${projectDir}/contract`, { veryVerbose, skip: skip.map(f => path.join(contractSourceDir, f)) })
81
118
 
119
+ // make out dir
120
+ fs.mkdirSync(`${projectDir}/out`)
121
+
82
122
  // changes in package.json for rust
83
123
  if (contract === 'rust') {
84
124
  await replaceInFiles({
85
125
  files: `${projectDir}/package.json`,
86
- from: 'cd contract && npm run build && mkdir -p ../out && rm -f ./out/main.wasm && cp ./build/release/greeter.wasm ../out/main.wasm',
87
- to: 'mkdir -p out && cd contract && rustup target add wasm32-unknown-unknown && cargo build --all --target wasm32-unknown-unknown --release && rm -f ./out/main.wasm && cp ./target/wasm32-unknown-unknown/release/greeter.wasm ../out/main.wasm'
126
+ from: 'cd contract && npm run build && cp ./build/release/greeter.wasm ../out/main.wasm',
127
+ to: 'cd contract && rustup target add wasm32-unknown-unknown && cargo build --all --target wasm32-unknown-unknown --release && cp ./target/wasm32-unknown-unknown/release/greeter.wasm ../out/main.wasm'
88
128
  })
89
129
  await replaceInFiles({
90
130
  files: `${projectDir}/package.json`,
@@ -108,10 +148,19 @@ const createProject = async function ({ contract, frontend, projectDir, veryVerb
108
148
 
109
149
  // setup rust
110
150
  let wasRustupInstalled = false
111
- if (contract === 'rust') {
151
+ if (contract === 'rust' || supports_sandbox) {
112
152
  wasRustupInstalled = await rustSetup.setupRustAndWasm32Target()
113
153
  }
114
154
 
155
+ if (contract === 'rust') {
156
+ // remove assemblyscript
157
+ await replaceInFiles({
158
+ files: `${projectDir}/package.json`,
159
+ from: '"near-sdk-as": "^3.2.3",',
160
+ to: ' '
161
+ })
162
+ }
163
+
115
164
  if (hasNpm || hasYarn) {
116
165
  console.log('Installing project dependencies...')
117
166
  spawn.sync(hasYarn ? 'yarn' : 'npm', ['install'], { cwd: projectDir, stdio: 'inherit' })
@@ -158,8 +207,8 @@ const opts = yargs
158
207
  .example('$0 new-app', 'Create a project called "new-app"')
159
208
  .option('frontend', {
160
209
  desc: 'template to use',
161
- choices: ['vanilla', 'react'],
162
- default: 'vanilla',
210
+ choices: ['vanilla', 'react', 'none'],
211
+ default: 'react',
163
212
  })
164
213
  .option('contract', {
165
214
  desc: 'language for smart contract',
@@ -0,0 +1,9 @@
1
+ require("util").inspect.defaultOptions.depth = 5; // Increase AVA's printing depth
2
+
3
+ module.exports = {
4
+ timeout: "300000",
5
+ files: ["src/*.ava.ts"],
6
+ failWithoutAssertions: false,
7
+ extensions: ["ts"],
8
+ require: ["ts-node/register"],
9
+ };
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "ava-testing",
3
+ "version": "1.0.0",
4
+ "license": "(MIT AND Apache-2.0)",
5
+ "scripts": {
6
+ "test": "ava --verbose"
7
+ },
8
+ "devDependencies": {
9
+ "ava": "^4.2.0",
10
+ "near-api-js": "^0.44.2",
11
+ "typescript": "^4.7.2",
12
+ "ts-node": "^10.8.0",
13
+ "@types/bn.js": "^5.1.0"
14
+ },
15
+ "dependencies": {}
16
+ }
@@ -0,0 +1,31 @@
1
+
2
+ import { keyStores, KeyPair } from 'near-api-js'
3
+ const fs = require('fs')
4
+
5
+ const CONTRACT_NAME=fs.readFileSync('../neardev/dev-account', 'utf-8')
6
+ const NETWORK_ID='testnet'
7
+
8
+ // Create an InMemoryKeyStore
9
+ const keyStore = new keyStores.InMemoryKeyStore()
10
+
11
+ // Load credentials
12
+ const credPath = `${process.env.HOME}/.near-credentials/${NETWORK_ID}/${CONTRACT_NAME}.json`
13
+ let credentials = JSON.parse(fs.readFileSync(credPath))
14
+
15
+ // Save key in the key store
16
+ keyStore.setKey(
17
+ NETWORK_ID,
18
+ CONTRACT_NAME,
19
+ KeyPair.fromString(credentials.private_key)
20
+ )
21
+
22
+ export const nearConfig = {
23
+ networkId: NETWORK_ID,
24
+ nodeUrl: 'https://rpc.testnet.near.org',
25
+ contractName: CONTRACT_NAME,
26
+ walletUrl: 'https://wallet.testnet.near.org',
27
+ helperUrl: 'https://helper.testnet.near.org',
28
+ explorerUrl: 'https://explorer.testnet.near.org',
29
+ headers: {},
30
+ deps: {keyStore}
31
+ }
@@ -0,0 +1,32 @@
1
+ import anyTest, { TestFn } from 'ava'
2
+
3
+ import { Near, Account, Contract } from 'near-api-js'
4
+ import { nearConfig } from './config'
5
+
6
+ const test = anyTest as TestFn<{
7
+ accounts: Record<string, any>;
8
+ }>
9
+
10
+ test.beforeEach(async (t) => {
11
+ const near = await new Near(nearConfig)
12
+ const user = await new Account(near.connection, nearConfig.contractName)
13
+ const contract = await new Contract(
14
+ user,
15
+ nearConfig.contractName,
16
+ { viewMethods: ['get_greeting'], changeMethods: ['set_greeting'] }
17
+ )
18
+ t.context.accounts = { contract }
19
+ })
20
+
21
+ test('returns the default greeting', async (t) => {
22
+ const { contract } = t.context.accounts
23
+ const message: string = await contract.get_greeting({})
24
+ t.is(message, 'Hello')
25
+ })
26
+
27
+ test('changes the message', async (t) => {
28
+ const { contract } = t.context.accounts
29
+ await contract.set_greeting({args:{ message: 'Howdy' }})
30
+ const message: string = await contract.get_greeting({})
31
+ t.is(message, 'Howdy')
32
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-near-app",
3
- "version": "3.0.0-pre.7.0",
3
+ "version": "3.1.0",
4
4
  "description": "generate new near blank project with different type",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -0,0 +1,2 @@
1
+ package-lock.json linguist-generated=true -diff
2
+ yarn.lock linguist-generated=true -diff
@@ -0,0 +1,6 @@
1
+ tasks:
2
+ - init: yarn
3
+ command: yarn dev
4
+ ports:
5
+ - port: 1234
6
+ onOpen: open-browser
@@ -0,0 +1,101 @@
1
+ near-blank-project
2
+ ==================
3
+
4
+ This app was initialized with [create-near-app]
5
+
6
+
7
+ Quick Start
8
+ ===========
9
+
10
+ To run this project locally:
11
+
12
+ 1. Prerequisites: Make sure you've installed [Node.js] ≥ 12
13
+ 2. Install dependencies: `npm install`
14
+ 3. Run the local development server: `npm run dev` (see `package.json` for a
15
+ full list of `scripts` you can run with `npm`)
16
+
17
+ Now you'll have a local development environment backed by the NEAR TestNet!
18
+
19
+ Go ahead and play with the app and the code. As you make code changes, the app will automatically reload.
20
+
21
+
22
+ Exploring The Code
23
+ ==================
24
+
25
+ 1. The "backend" code lives in the `/contract` folder. See the README there for
26
+ more info.
27
+ 2. The frontend code lives in the `/frontend` folder. `/frontend/index.html` is a great
28
+ place to start exploring. Note that it loads in `/frontend/assets/src/js/index.js`, where you
29
+ can learn how the frontend connects to the NEAR blockchain.
30
+ 3. Tests: there are different kinds of tests for the frontend and the smart
31
+ contract. See `contract/README` for info about how it's tested. The frontend
32
+ code gets tested with [jest]. You can run both of these at once with `npm
33
+ run test`.
34
+
35
+
36
+ Deploy
37
+ ======
38
+
39
+ Every smart contract in NEAR has its [own associated account][NEAR accounts]. When you run `npm run dev`, your smart contract gets deployed to the live NEAR TestNet with a throwaway account. When you're ready to make it permanent, here's how.
40
+
41
+
42
+ Step 0: Install near-cli (optional)
43
+ -------------------------------------
44
+
45
+ [near-cli] is a command line interface (CLI) for interacting with the NEAR blockchain. It was installed to the local `node_modules` folder when you ran `npm install`, but for best ergonomics you may want to install it globally:
46
+
47
+ npm install --global near-cli
48
+
49
+ Or, if you'd rather use the locally-installed version, you can prefix all `near` commands with `npx`
50
+
51
+ Ensure that it's installed with `near --version` (or `npx near --version`)
52
+
53
+
54
+ Step 1: Create an account for the contract
55
+ ------------------------------------------
56
+
57
+ Each account on NEAR can have at most one contract deployed to it. If you've already created an account such as `your-name.testnet`, you can deploy your contract to `near-blank-project.your-name.testnet`. Assuming you've already created an account on [NEAR Wallet], here's how to create `near-blank-project.your-name.testnet`:
58
+
59
+ 1. Authorize NEAR CLI, following the commands it gives you:
60
+
61
+ near login
62
+
63
+ 2. Create a subaccount (replace `YOUR-NAME` below with your actual account name):
64
+
65
+ near create-account near-blank-project.YOUR-NAME.testnet --masterAccount YOUR-NAME.testnet
66
+
67
+
68
+ Step 2: set contract name in code
69
+ ---------------------------------
70
+
71
+ Modify the line in `src/config.js` that sets the account name of the contract. Set it to the account id you used above.
72
+
73
+ const CONTRACT_NAME = process.env.CONTRACT_NAME || 'near-blank-project.YOUR-NAME.testnet'
74
+
75
+
76
+ Step 3: deploy!
77
+ ---------------
78
+
79
+ One command:
80
+
81
+ npm run deploy
82
+
83
+ As you can see in `package.json`, this does two things:
84
+
85
+ 1. builds & deploys smart contract to NEAR TestNet
86
+ 2. builds & deploys frontend code to GitHub using [gh-pages]. This will only work if the project already has a repository set up on GitHub. Feel free to modify the `deploy` script in `package.json` to deploy elsewhere.
87
+
88
+
89
+ Troubleshooting
90
+ ===============
91
+
92
+ On Windows, if you're seeing an error containing `EPERM` it may be related to spaces in your path. Please see [this issue](https://github.com/zkat/npx/issues/209) for more details.
93
+
94
+
95
+ [create-near-app]: https://github.com/near/create-near-app
96
+ [Node.js]: https://nodejs.org/en/download/package-manager/
97
+ [jest]: https://jestjs.io/
98
+ [NEAR accounts]: https://docs.near.org/docs/concepts/account
99
+ [NEAR Wallet]: https://wallet.testnet.near.org/
100
+ [near-cli]: https://github.com/near/near-cli
101
+ [gh-pages]: https://github.com/tschaub/gh-pages
@@ -0,0 +1,27 @@
1
+ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2
+ # Developer note: near.gitignore will be renamed to .gitignore upon project creation
3
+ # dependencies
4
+ node_modules
5
+ /.pnp
6
+ .pnp.js
7
+ /out
8
+
9
+ #keys
10
+ /neardev
11
+
12
+ # testing
13
+ /coverage
14
+
15
+ # production
16
+ /build
17
+
18
+ # misc
19
+ .DS_Store
20
+ .env.local
21
+ .env.development.local
22
+ .env.test.local
23
+ .env.production.local
24
+
25
+ npm-debug.log*
26
+ yarn-debug.log*
27
+ yarn-error.log*
@@ -0,0 +1 @@
1
+ {"account_id":"test.near","private_key":"ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw"}
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "greeter",
3
+ "version": "1.0.0",
4
+ "license": "(MIT AND Apache-2.0)",
5
+ "scripts": {
6
+ "build": "npm run build:contract",
7
+ "build:contract": "cd contract && npm run build && cp ./build/release/greeter.wasm ../out/main.wasm",
8
+ "deploy": "npm run build:contract && near dev-deploy",
9
+ "start": "npm run deploy",
10
+ "dev": "nodemon --watch contract -e ts --exec \"npm run start\"",
11
+ "test": "npm run build:contract && npm run test:unit && npm run test:integration",
12
+ "test:unit": "cd contract && npm i && npm run test",
13
+ "test:integration": "npm run test:integration:ts && npm run test:integration:rs",
14
+ "test:integration:ts": "cd integration-tests/ts && npm run test",
15
+ "test:integration:rs": "cd integration-tests/rs && cargo run --example integration-tests"
16
+ },
17
+ "devDependencies": {
18
+ "env-cmd": "^10.1.0",
19
+ "near-cli": "^3.3.0",
20
+ "nodemon": "~2.0.16",
21
+ "parcel": "^2.6.0",
22
+ "ava": "^4.2.0",
23
+ "near-workspaces": "^2.0.0",
24
+ "typescript": "^4.7.2",
25
+ "process": "^0.11.10",
26
+ "ts-node": "^10.8.0"
27
+ },
28
+ "dependencies": {
29
+ "near-api-js": "^0.44.2",
30
+ "near-sdk-as": "^3.2.3",
31
+ "regenerator-runtime": "~0.13.9"
32
+ }
33
+ }
@@ -4,9 +4,9 @@
4
4
  "license": "(MIT AND Apache-2.0)",
5
5
  "scripts": {
6
6
  "build": "npm run build:contract && npm run build:web",
7
- "build:contract": "cd contract && npm run build && mkdir -p ../out && rm -f ./out/main.wasm && cp ./build/release/greeter.wasm ../out/main.wasm",
7
+ "build:contract": "cd contract && npm run build && cp ./build/release/greeter.wasm ../out/main.wasm",
8
8
  "build:web": "parcel build frontend/index.html --public-url ./",
9
- "deploy": "npm run build && near dev-deploy",
9
+ "deploy": "npm run build:contract && near dev-deploy",
10
10
  "start": "npm run deploy && echo The app is starting! It will automatically open in your browser when ready && env-cmd -f ./neardev/dev-account.env parcel frontend/index.html --open",
11
11
  "dev": "nodemon --watch contract -e ts --exec \"npm run start\"",
12
12
  "test": "npm run build:contract && npm run test:unit && npm run test:integration",
@@ -4,9 +4,9 @@
4
4
  "license": "(MIT AND Apache-2.0)",
5
5
  "scripts": {
6
6
  "build": "npm run build:contract && npm run build:web",
7
- "build:contract": "cd contract && npm run build && mkdir -p ../out && rm -f ./out/main.wasm && cp ./build/release/greeter.wasm ../out/main.wasm",
7
+ "build:contract": "cd contract && npm run build && cp ./build/release/greeter.wasm ../out/main.wasm",
8
8
  "build:web": "parcel build frontend/index.html --public-url ./",
9
- "deploy": "npm run build && near dev-deploy",
9
+ "deploy": "npm run build:contract && near dev-deploy",
10
10
  "start": "npm run deploy && echo The app is starting! && env-cmd -f ./neardev/dev-account.env parcel frontend/index.html --open",
11
11
  "dev": "nodemon --watch contract -e ts --exec \"npm run start\"",
12
12
  "test": "npm run build:contract && npm run test:unit && npm run test:integration",
@@ -1,4 +1,5 @@
1
1
  const chalk = require('chalk')
2
+ const os = require('os')
2
3
  const readline = require('readline')
3
4
  const sh = require('shelljs')
4
5
 
@@ -6,13 +7,14 @@ const sh = require('shelljs')
6
7
  const installRustupScript = 'curl --proto \'=https\' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y'
7
8
  const updatePath = '. $HOME/.cargo/env'
8
9
  const addWasm32TargetScript = 'rustup target add wasm32-unknown-unknown'
9
-
10
10
  // We should update PATH in the same script because every new Bash scripts are executed in a separate shell
11
11
  const updatePathAndAddWasm32TargetScript = updatePath + ' && ' + addWasm32TargetScript
12
12
 
13
+ const isWindows = os.platform() === 'win32'
14
+
13
15
  function isRustupInstalled() {
14
16
  console.log(chalk`Checking if {bold rustup} is installed...`)
15
- const isInstalled = sh.exec('rustup --version &> /dev/null').code === 0
17
+ const isInstalled = sh.exec('rustup --version').code === 0
16
18
  console.log(chalk`{bold rustup} is`, isInstalled ? 'installed\n' : 'not installed\n')
17
19
  return isInstalled
18
20
  }
@@ -69,21 +71,30 @@ const addWasm32TargetDisclaimer = chalk`To build Rust smart contracts you need t
69
71
 
70
72
  const installRustupQuestion = chalk`
71
73
  ${installRustupDisclaimer} We can run the following command to do it for you:
72
-
73
74
  {bold ${installRustupScript}}
74
-
75
75
  Continue with installation (Y/n)?: `
76
76
 
77
77
  const addWasm32TargetQuestion = chalk`
78
78
  ${addWasm32TargetDisclaimer} We can run the following command to do it for you:
79
-
80
79
  {bold ${addWasm32TargetScript}}
81
-
82
80
  Continue with installation (Y/n)?: `
83
81
 
82
+ const rustupAndWasm32WindowsInstallationInstructions = chalk`
83
+ ${installRustupDisclaimer}
84
+ 1. Go to https://rustup.rs
85
+ 2. Download {bold rustup-init.exe}
86
+ 3. Install it on your system
87
+ ${addWasm32TargetDisclaimer}
88
+ Run the following command to do it:
89
+ {bold ${addWasm32TargetScript}}
90
+ Press {bold Enter} to continue project creation.`
84
91
 
85
92
  async function setupRustAndWasm32Target() {
86
93
  try {
94
+ if (isWindows) {
95
+ await askYesNoQuestionAndRunFunction(rustupAndWasm32WindowsInstallationInstructions)
96
+ return false
97
+ }
87
98
  let wasRustupInstalled = false
88
99
  if (!isRustupInstalled()) {
89
100
  wasRustupInstalled = await askYesNoQuestionAndRunFunction(installRustupQuestion, installRustup)
@@ -99,4 +110,4 @@ async function setupRustAndWasm32Target() {
99
110
 
100
111
  module.exports = {
101
112
  setupRustAndWasm32Target,
102
- }
113
+ }